0

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

0

3 Answers 3

2

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]
Sign up to request clarification or add additional context in comments.

Comments

1

You can compare it to string:

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

Output:

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

Comments

1

Notice you can always convert to tuple

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.