Python pandas:从dataframe中删除不匹配多个条件的行

时间:2017-08-08 03:10:41

标签: python pandas

假设我有一个使用pandas.dataframe的列,如下所示:

index  fruits    origin      attribute
 1     apple     USA         tasty
 2     apple     France      yummy
 3     apple     USA         juicy
 4     apple     England     juicy
 5     apple     Japan       normal
 6     banana    Canada      nice
 7     banana    Italy       good
 .....

我想选择yummy apple from France(2)并从表中移除不匹配的apples,如下所示:

index  fruits    origin      attribute
 1     apple     France      yummy
 2     banana    Canada      nice
 3     banana    Italy       good
 .....

我认为以下内容应该有效。但事实并非如此:

df.drop(df[(df.fruits == "apple") & (df.origin != "France") | (df.fruits == "apple") & (df.attribute != "yummy")].index)

然后我尝试了以下哪些也行不通:

df = df[~df[(df.fruits == "apple") & (df.origin != "France") & (df.attribute != "yummy")]

任何帮助,小伙子?

2 个答案:

答案 0 :(得分:3)

如果通过匹配条件选择:

df[(df.fruits != 'apple') | ((df.fruits == 'apple') & (df.origin == 'France') & (df.attribute == 'yummy'))]

#index  fruits  origin  attribute
#1  2    apple  France      yummy
#5  6   banana  Canada       nice
#6  7   banana   Italy       good

如果按不匹配条件删除:需要删除的内容是fruits为苹果但origin不匹配Franceattribute不包含的行#39; t匹配yummy

df[~((df.fruits == 'apple') & ((df.origin != 'France') | (df.attribute != 'yummy')))]

# index fruits  origin  attribute
#1    2  apple  France      yummy
#5    6 banana  Canada       nice
#6    7 banana   Italy       good

答案 1 :(得分:1)

df.query(
    'fruits == "apple" & origin == "France" & attribute == "yummy"'
).append(df.query('fruits != "apple"'))

       fruits  origin attribute
index                          
2       apple  France     yummy
6      banana  Canada      nice
7      banana   Italy      good