pandas 如何在 python 中为熊猫创建“非”过滤器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37493723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:18:26  来源:igfitidea点击:

How do I create a "not" filter in python for pandas

pythonpython-2.7pandas

提问by staten12

I have this large dataframe I've imported into pandas and I want to chop it down via a filter. Here is my basic sample code:

我有这个大数据框,我已经导入到 Pandas 中,我想通过过滤器将其切碎。这是我的基本示例代码:

import pandas as pd
import numpy as np
from pandas import Series, DataFrame

df = DataFrame({'A':[12345,0,3005,0,0,16455,16454,10694,3005],'B':[0,0,0,1,2,4,3,5,6]})

df2= df[df["A"].map(lambda x: x > 0) & (df["B"] > 0)]

Basically this displays bottom 4 results which is semi-correct. But I need to display everything BUT these results. So essentially, I'm looking for a way to use this filter but in a "not" version if that's possible. So if column A is greater than 0 AND column B is greater than 0 then we want to disqualify these values from the dataframe. Thanks

基本上这显示了半正确的后 4 个结果。但我需要显示除这些结果之外的所有内容。所以基本上,我正在寻找一种使用此过滤器的方法,但如果可能的话,使用“非”版本。因此,如果 A 列大于 0 且 B 列大于 0,那么我们希望从数据框中取消这些值。谢谢

回答by hhbilly

No need for map function call on Series "A".

不需要在系列“A”上调用 map 函数。

Apply De Morgan's Law:

应用德摩根定律

"not (A and B)" is the same as "(not A) or (not B)"

"not (A and B)" 等同于 "(not A) or (not B)"

df2 = df[~(df.A > 0) | ~(df.B > 0)]

回答by ssm

There is no need for the mapimplementation. You can just reverse the arguments like ...

不需要map执行。你可以颠倒这样的论点......

df.ix[(df.A<=0)|(df.B<=0),:]

Or use boolean indexingwithout ix:

boolean indexing不使用ix

df[(df.A<=0)|(df.B<=0)]

回答by piRSquared

Try

尝试

df2 = df[df["A"].map(lambda x: x <= 0) | (df["B"] <= 0)]