如何在 Pandas DataFrame 中使用 inside / in 运算符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31595146/
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-13 23:40:12  来源:igfitidea点击:

How do I use within / in operator in a Pandas DataFrame?

pythonpandas

提问by Suzuki Soma

I want to select data from my CSV file.

我想从我的 CSV 文件中选择数据。

Though I can get a data in which column

虽然我可以得到哪一列的数据

"House" == 1 (any single number) 

as following, I don't know how to get the data where

如下,我不知道如何获取数据

"House" in [1, 2, 3, 4, 6, 7, 8, 9, 10, 13, 17, 18, 20, 21, 23, 26, 28, 30, 34, 46, 57, 58, 61, 86, 89, 102, 121, 156].
df = pd.read_csv('../../data/training_dataset_500.csv')
df[df['House']==1]

enter image description here

在此处输入图片说明

回答by Anand S Kumar

Use the Series.isin()method to check if a series value is in a list of values. In your case -

使用该Series.isin()方法检查系列值是否在值列表中。在你的情况下 -

df[df['House'].isin([1, 2, 3, 4, 6, 7, 8, 9, 10, 13, 17, 18, 20, 21, 23, 26, 28, 30, 34, 46, 57, 58, 61, 86, 89, 102, 121, 156])]

Example -

例子 -

In [77]: df
Out[77]:
   A  B
0  1  5
1  2  6
2  3  7
3  4  8

In [78]: df[df['A'].isin([1,2])]
Out[78]:
   A  B
0  1  5
1  2  6