Drop rows on multiple conditions in pandas dataframe

匿名 (未验证) 提交于 2019-12-03 01:38:01

问题:

My df has 3 columns

df = pd.DataFrame({"col_1": (0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0),                     "col_2": (0.0, 0.24, 1.0, 0.0, 0.22, 3.11, 0.0),                     "col_3": ("Mon", "Tue", "Thu", "Fri", "Mon", "Tue", "Thu")})  

I want to drop rows where df.col_1 is 1.0 and df.col_2 is 0.0. So, I would get:

df = pd.DataFrame({"col_1": (0.0, 0.0, 1.0, 0.0, 1.0),                     "col_2": (0.0, 0.24, 1.0, 0.22, 3.11),                     "col_3": ("Mon", "Tue", "Thu", "Mon", "Tue")}) 

I tried:

df_new = df.drop[df[(df['col_1'] == 1.0) & (df['col_2'] == 0.0)].index] 

It gives me the error:

'method' object is not subscriptable 

Any idea how to solve the above problem?

回答1:

drop is a method, you are calling it using [], that is why it gives you:

'method' object is not subscriptable 

change to () (a normal method call) an it should work:

import pandas as pd  df = pd.DataFrame({"col_1": (0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0),                    "col_2": (0.0, 0.24, 1.0, 0.0, 0.22, 3.11, 0.0),                    "col_3": ("Mon", "Tue", "Thu", "Fri", "Mon", "Tue", "Thu")})  df_new = df.drop(df[(df['col_1'] == 1.0) & (df['col_2'] == 0.0)].index) print(df_new) 

Output

   col_1  col_2 col_3 0    0.0   0.00   Mon 1    0.0   0.24   Tue 2    1.0   1.00   Thu 4    0.0   0.22   Mon 5    1.0   3.11   Tue 


回答2:

Try to filter your df with loc. It's so powerfull. The "~" means you want the opposit of your condition. The ":" means you want to keep all the columns

df = df.loc[~((df['col_1'] == 1.0) & (df['col_2'] == 0.0)),:] 


回答3:

You can use or (|) operator for this , Refer this link for it pandas: multiple conditions while indexing data frame - unexpected behavior

i.e dropping rows where both conditions are met

 df = df.loc[~((df['col_1']==1) | (df['col_2']==0))] 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!