简体   繁体   English

Python Pandas:从数据框中删除不符合多个条件的行

[英]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是苹果但originFrance不匹配或attributeyummy不匹配的行:

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.

相关问题 熊猫:从DataFrame匹配条件中删除行 - Pandas: Delete Rows From DataFrame Matching Conditions 从python pandas中的DataFrame中删除特定行 - removing particular rows from DataFrame in python pandas 根据其他行和列的多个条件在数据框中创建新列? 包括空行? - 蟒蛇/熊猫 - Creating a new column in dataframe based on multiple conditions from other rows and columns? Including rows that are null? - Python/Pandas 如何从满足多个条件的数据框中删除特定行(python pandas)? - How to remove specific rows from a dataframe satisfying multiple conditions (python pandas)? pandas dataframe - Python 中的多个 if 条件 - Multiple if conditions in pandas dataframe - Python 熊猫基于多个条件从数据框中删除行,而没有for循环 - pandas remove rows from dataframe based on multiple conditions without for loops 有没有更好的方法来基于多个条件从 pandas DataFrame 行 select 行? - Is there a better way to select rows from a pandas DataFrame based on multiple conditions? 从 pandas 数据框中选择具有多个条件的非 NaN 行 - select non-NaN rows with multiple conditions from a pandas dataframe 根据多个条件从 pandas dataframe 中删除具有 NaN 的行 - Drop rows with NaNs from pandas dataframe based on multiple conditions pandas:选择符合多个条件的所有行 - pandas: select all rows matching multiple conditions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM