[英]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.