在不满足条件的所有行上过滤 Pandas 数据框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39078781/
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
filter a pandas data frame on all rows that do NOT meet a condition
提问by seth127
This seems simple, but I can't seem to figure it out. I know how to filter a pandas data frame to all rows that meet a condition, but when I want the opposite, I keep getting weird errors.
这看起来很简单,但我似乎无法弄清楚。我知道如何将 Pandas 数据框过滤到满足条件的所有行,但是当我想要相反的时候,我不断收到奇怪的错误。
Here is the example. (Context: a simple board game where pieces are on a grid and we're trying to give it a coordinate and return all adjacent pieces, but NOT the actual piece on that actual coordinate)
这是示例。(上下文:一个简单的棋盘游戏,棋子在网格上,我们试图给它一个坐标并返回所有相邻的棋子,但不是该实际坐标上的实际棋子)
import pandas as pd
import numpy as np
df = pd.DataFrame([[5,7, 'wolf'],
[5,6,'cow'],
[8, 2, 'rabbit'],
[5, 3, 'rabbit'],
[3, 2, 'cow'],
[7, 5, 'rabbit']],
columns = ['lat', 'long', 'type'])
coords = [5,7] #the coordinate I'm testing, a wolf
view = df[((coords[0] - 1) <= df['lat']) & (df['lat'] <= (coords[0] + 1)) \
& ((coords[1] - 1) <= df['long']) & (df['long'] <= (coords[1] + 1))]
view = view[not ((coords[0] == view['lat']) & (coords[1] == view['long'])) ]
print(view)
I thought the not
should just negate the boolean inside the parentheses that followed, but this doesn't seem to be how it works.
我认为not
应该只是否定后面括号内的布尔值,但这似乎不是它的工作原理。
I want it to return the cow at 5,6 but NOT the wolf at 5,7 (because that's the current piece). Just to double check my logic, I did
我希望它在 5,6 处返回母牛,而不是在 5,7 处返回狼(因为这是当前的部分)。只是为了仔细检查我的逻辑,我做到了
me = view[(coords[0] == view['lat']) & (coords[1] == view['long'])]
print(me)
and this returned just the wolf, as I'd expected. So why can't I just put a not
in front of that and get everything else? Or, more importantly, what do I do instead to get everything else.
正如我所料,这只是狼回来了。那么为什么我不能not
在它前面放一个并获得其他所有东西呢?或者,更重要的是,我该怎么做才能获得其他一切。
回答by DeepSpace
As numpy
(therefore pandas
) use bitwise operators, you should replace not
with ~
. This is also the reason you are using &
and not and
.
由于numpy
(因此pandas
)使用按位运算符,您应该替换not
为~
。这也是您使用&
而不是的原因and
。
import pandas as pd
df = pd.DataFrame({'a': [1, 2]})
print(df[~(df['a'] == 1)])
>> a
1 2
And using your example:
并使用您的示例:
import pandas as pd
import numpy as np
df = pd.DataFrame([[5,7, 'wolf'],
[5,6,'cow'],
[8, 2, 'rabbit'],
[5, 3, 'rabbit'],
[3, 2, 'cow'],
[7, 5, 'rabbit']],
columns = ['lat', 'long', 'type'])
coords = [5,7] #the coordinate I'm testing, a wolf
view = df[((coords[0] - 1) <= df['lat']) & (df['lat'] <= (coords[0] + 1)) \
& ((coords[1] - 1) <= df['long']) & (df['long'] <= (coords[1] + 1))]
view = view[~ ((coords[0] == view['lat']) & (coords[1] == view['long'])) ]
print(view)
>> lat long type
1 5 6 cow