1

I would like to remove rows who contain a string / value in a column (not a specific string). In other words, if a column is filled with any kind of value the row should be deleted completely.

In this example the the row 2 and Row 4 should be deleted because in column secret there is a value

output expected

data = [['Alex',10,''],['John',12,'Name is Secret'],['Ben',13,''],['Steeve',13,'1']]
df = pd.DataFrame(data,columns=['Name','Age','secret'])

indexDelete = df[ df['secret'] == '*'].index

df.drop(indexDelete , inplace=True)
1
  • The rows you want to keep still contain empty strings. You could filter to just empty strings if this is expected or do you want to only keep rows that have NaN in the cell? Commented Dec 16, 2022 at 20:21

1 Answer 1

1

You can try this:

df[df['secret'].str.len() == 0]

This removes any rows that have empty string as a value.

Not sure that this is exactly what you are looking for.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.