drop rows in pandas dataframe that are not meeting the conditions

Chique_Code :

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!

cheebz :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=24929&siteId=1