pandas 使用python中的列表值过滤匹配列值的数据框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53082014/
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 dataframe matching column values with list values in python
提问by Rubén
I have a DataFrame
like the following:
我有一个DataFrame
喜欢以下内容:
import numpy as np
import pandas as pd
import string
import random
random.seed(42)
df = pd.DataFrame({'col1': list(string.ascii_lowercase)[:11],
'col2':[random.randint(1,100) for x in range(11)]})
df
col1 col2
0 a 64
1 b 3
2 c 28
3 d 23
4 e 74
5 f 68
6 g 90
7 h 9
8 i 43
9 j 3
10 k 22
I'm trying to create a new dataframe based on the filtering the rows of the previous dataframe that match a list of values. I have tried the next piece of code:
我正在尝试基于过滤匹配值列表的前一个数据帧的行来创建一个新的数据帧。我已经尝试了下一段代码:
df_filt = df[df['col1'] in ['a','c','h']]
But I get an error. I'm expecting the next result:
但我得到一个错误。我期待下一个结果:
df_filt
col1 col2
0 a 64
1 c 28
2 h 9
I'm looking for a flexible solution that allows to filter based on more elements of the matching list than the ones presented in the example.
我正在寻找一种灵活的解决方案,该解决方案允许基于匹配列表中比示例中显示的元素更多的元素进行过滤。
回答by timgeb
You can use pandas.Series.isin
for compound "in"-checks.
您可以pandas.Series.isin
用于复合“输入”检查。
Input dataframe:
输入数据框:
>>> df
>>>
col1 col2
0 a 64
1 b 3
2 c 28
3 d 23
4 e 74
5 f 68
6 g 90
7 h 9
8 i 43
9 j 3
10 k 22
Output dataframe:
输出数据帧:
>>> df[df['col1'].isin(['a', 'c', 'h'])]
>>>
col1 col2
0 a 64
2 c 28
7 h 9
回答by Dani G
Use isin
用 isin
df_filt = df[df.col1.isin(['a','c','h'])]