简体   繁体   English

使用相关条件过滤DataFrame中的行

[英]Filtering rows in DataFrame with dependent conditions

Apologies if this has already been asked: 抱歉,如果已经有人问过:

I want to remove all rows with values between 15-25 in one column AND have a specific string in another column. 我想删除一列中值在15-25之间的所有行,并在另一列中删除特定的字符串。

For example: 例如:

options = ['pizza', 'pasta'] 

df2 = df[(~df['columnA'].between(15, 25)) & df.loc[~df['columnB'].isin(options)]]

So if a row has a value of 15-25 in columnA but does not have 'pizza' or 'pasta' in columnB, then I want that row to be retained... 因此,如果某行在columnA中的值为15-25,但在columnB中没有“ pizza”或“ pasta”,那么我希望保留该行...

Solution: 解:

df[~((df['columnA'].between(15, 25)) & (df['columnB'].isin(options)))]

The easiest to understand is negating the entire condition, like ~((...) & (...)) : 最容易理解的是求反整个条件,例如~((...) & (...))

df[~((df['columnA'].between(15, 25)) & (df['columnB'].isin(options)))]

Or you can use De Morgan's laws [wiki] , and specify this as (~ ...) | (~ ...) 或者,您可以使用De Morgan的定律 [wiki]并将其指定为(~ ...) | (~ ...) (~ ...) | (~ ...)

df[(~df['columnA'].between(15, 25)) | (~df['columnB'].isin(options))]

So the negation of x ∧ y is (¬x)∨(¬y) . 因此x y的否定是(¬x)∨(¬y)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM