Drop rows with specific string value pandas

Viewed 9975

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?

2 Answers

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)

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([]).

Related