[英]Python pandas: removing rows not matching multiple conditions from dataframe
假设我有一个使用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)
选择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")]
伙计们,有什么帮助吗?
如果通过匹配条件选择:
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
与France
不匹配或attribute
与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
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.