Python Pandas DataFrame:访问多个不等于,=!
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34397982/
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
Pandas DataFrame: access multiple items with not equal to, =!
提问by JianguoHisiang
I have the following Pandas DataFrame object df
. It is a train schedule listing the date of departure, scheduled time of departure, and train company.
我有以下 Pandas DataFrame 对象df
。它是列有出发日期、预定出发时间和列车公司的列车时刻表。
import pandas as pd
df =
Year Month DayofMonth DayOfWeek DepartureTime Train Origin
Datetime
1988-01-01 1988 1 1 5 1457 BritishRail Leeds
1988-01-02 1988 1 2 6 1458 DeutscheBahn Berlin
1988-01-03 1988 1 3 7 1459 SNCF Lyons
1988-01-02 1988 1 2 6 1501 BritishRail Ipswich
1988-01-02 1988 1 2 6 1503 NMBS Brussels
....
Now, let's say I wanted to select all items "DeutscheBahn" in the column "Train".
现在,假设我想选择“火车”列中的所有项目“DeutscheBahn”。
I would use
我会用
DB = df[df['Train'] == 'DeutscheBahn']
Now, how can I select all trains except DeutscheBahn and British Rails and SNCF. How can I simultaneously choose the items notthese?
现在,我如何选择除 DeutscheBahn 和 British Rails 和 SNCF 之外的所有列车。我如何能同时选择的项目不是这些?
notDB = df[df['Train'] != 'DeutscheBahn']
and
和
notSNCF = df[df['Train'] != 'SNCF']
but I am not sure how to combine these into one command.
但我不确定如何将这些组合成一个命令。
df[df['Train'] != 'DeutscheBahn', 'SNCF']
doesn't work.
不起作用。
采纳答案by DeepSpace
df[~df['Train'].isin(['DeutscheBahn', 'SNCF'])]
isin
returns the values in df['Train']
that are in the given list, and the ~
at the beginning is essentially a not
operator.
isin
返回df['Train']
给定列表中的值,~
开头的本质上是一个not
运算符。
Another working but longer syntax would be:
另一个有效但更长的语法是:
df[(df['Train'] != 'DeutscheBahn') & (df['Train'] != 'SNCF')]
回答by DrGabrielA81
I like using the query method as it's a bit more clear
我喜欢使用查询方法,因为它更清晰一点
df = df.query("Train not in ['DeutscheBahn', 'British Rails', 'SNCF']")