简体   繁体   中英

Filter out rows based on multiple conditions in pandas

I can identify the rows I want to remove from a dataframe using the following code:

df[(df.Year.isin(range(200,205))) & (df.id== 'string')]

How do I remove these rows from the dataframe?

I tried the following solution but it didn't work:

df.drop[(df.Year.isin(range(200,205))) & (df.id== 'string'), axis = 0]

You can drop the rows by index as well

df.drop(df[(df.Year.isin(range(200,205))) & (df.id== 'string')].index, inplace=True)

Another way suggested by @JoshFriedlander

df = df[~((df.Year.isin(range(200,205))) & (df.id== 'string'))]

my go to solution would be:

df = df[(df['year'] >= 200) & (df['year'] <= 205) & (df['id'] == 'string')]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM