[英]Python pandas: removing rows not matching multiple conditions from dataframe
Suppose I have a column using pandas.dataframe like so: 假设我有一个使用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
.....
I want to select yummy apple from France(2)
and remove unmatching apples
from table like the following: 我想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
.....
I thought the following should work. 我认为以下应该可行。 But it doesn't: 但事实并非如此:
df.drop(df[(df.fruits == "apple") & (df.origin != "France") | (df.fruits == "apple") & (df.attribute != "yummy")].index)
Then I tried the following which also doesn't work: 然后,我尝试了以下同样行不通的方法:
df = df[~df[(df.fruits == "apple") & (df.origin != "France") & (df.attribute != "yummy")]
Any help, lads? 伙计们,有什么帮助吗?
If select by matching condition: 如果通过匹配条件选择:
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
If remove by non-matching condition: what needs to be removed are rows where fruits
is apple but origin
doesn't match France
or attribute
doesn't match yummy
: 如果按不匹配条件删除:需要删除的是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.