Pandas loc multiple conditions [duplicate]

泄露秘密 提交于 2019-12-20 03:19:55

问题


I have a dataframe and I want to delete all rows where column A is equal to blue and also col B is equal to green.

I though the below should work, but its not the case.

Can anyone see the problem

df=df.loc[~(df['A']=='blue' & df['B']=='green')]

回答1:


You should separate the two propositions:

df1=df.loc[~(df['A']=='blue') & ~(df['B']=='green')]



回答2:


use eq instead of ==:

df.loc[~(df['A'].eq('blue') & df['B'].eq('green'))]



回答3:


query

Note the != and or as consequence of De Morgan's Law

df.query('A != "blue" or B != "green"')


来源:https://stackoverflow.com/questions/56890859/pandas-loc-multiple-conditions

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