Home > Back-end >  Pandas finding rows from column of list
Pandas finding rows from column of list

Time:06-08

I have a large dataframe df which looks as following, where each value in column Col2 is itself a list:

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

CodePudding user response:

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.

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]

CodePudding user response:

You can compare it to string:

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

Output:

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

CodePudding user response:

Notice you can always convert to tuple

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