Christian van Aalst
Christian van Aalst

Reputation: 25

Drop rows with specific string value pandas

I have some a df with bridge data where in certain columns the string "(R)" is in. I want to remove those rows if "Blabla (R)" or "hello (R)" is the value. (so something with str.contains)

enter image description here

As in the picture, i want to drop row 9. (and many more)

Why is this error given and how to fix this?

Upvotes: 2

Views: 12657

Answers (2)

mcbridecaleb
mcbridecaleb

Reputation: 101

As mentioned in the question, str.contains is a useful tool for a wildcard search.

df.drop(df[df['name'].str.contains('R')].index.tolist())

.index returns a Int64Index that is not iterable so .tolist() converts this to a list and is finally ready for df.drop([]).

Upvotes: 0

Wayne Lambert
Wayne Lambert

Reputation: 606

If you would like to remove any rows where the road field in the df dataframe equals (S), then you can use the following code. This will remove these rows within the same dataframe (df)

df.drop(index=df[df['road'] == '(R)'].index, inplace=True)

Upvotes: 3

Related Questions