简体   繁体   English

熊猫从列表的列中查找行

[英]Pandas finding rows from column of list

I have a large dataframe df which looks as following, where each value in column Col2 is itself a list:我有一个大数据框df ,如下所示,其中Col2列中的每个值本身就是一个列表:

Col1  Col2
R1    ['C1']
R2    ['C1', 'C2']
R3    ['C1']

I want to get the following:我想得到以下信息:

Col1  Col2
R1    ['C1']
R3    ['C1']

I am trying the following:我正在尝试以下操作:

df[df['Col2'] == ['C1']]

But it is not generating desired results.但它没有产生预期的结果。

Edit: I am trying to get the rows where Col2 contains the list with only values ['C1'] and not ['C1', 'C2'] , etc编辑:我正在尝试获取Col2包含只有值['C1']而不是['C1', 'C2']等的列表的行

You can't use the equal operator with a list as pandas will try to use the list as a vector to match all elements of the Series.您不能将等号运算符与列表一起使用,因为 pandas 会尝试将列表用作向量来匹配系列的所有元素。

Assuming you have a Series of lists, you can use:假设您有一系列列表,您可以使用:

df[[x==['C1'] for x in df['Col2']]]

or:或者:

df[df['Col2'].str[0].eq('C1') & df['Col2'].str.len().eq(1)]

output:输出:

  Col1  Col2
0   R1  [C1]
2   R3  [C1]

You can compare it to string:您可以将其与字符串进行比较:

df[df['Col2'].astype(str).eq("['C1']")]

Output:输出:

  Col1  Col2
0   R1  [C1]
2   R3  [C1]

请注意,您始终可以转换为tuple

df[df['Col2'].map(tuple)==('C1',)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM