drop rows in pandas dataframe that are not meeting the conditions

Viewed 866

I have a pandas dataframe and need to clean Status column. My data looks like this:

id        Status
123       100%
124       0%
125       1%
126       100%
127       0.25%

I want to exclude all the rows that are NOT 100% or 0%. The type of the column is object

I want my data looks like this:

id        Status
123       100%
124       0%
126       100%

I have tried the following:

df = df.drop(df[(df.Status == '100%') & (df.Status == '0%')].index)

But that actually doesn't change the dataset at all.

Thank you!

3 Answers

You can conditionally select the rows that meet your criteria and set that as the new dataframe value

df = df.loc[(df['Status']  == '100%') | (df['Status']  == '0%')]

Edit: "|" instead of "&" since both cannot be true at the same time, thus returning 0 results.

First, you say "or", so you need the pipe operator, not ampersand (even with correct coding, "Status" will never be both 0% and 100%). Second, you correctly call the index from your conditions, but you need to pass the index to drop as an index. This should work:

df.drop(index=df[(df.Status=="100%" )| (df.Status=="0%")].index)
df = pd.DataFrame([['100%'], ['0%'], ['1%'],['100%'],['0.25%']], columns=['Status'], index=[123, 124,125,126,127])
df = df[(df.Status == '100%') | (df.Status == '0%')]
Related