问题
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