Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading