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

How to check data inside column pandas

i have a df and a list

Value   Dummy   
AB,AE     2
AE,RF,AW  2
AC,AD     2

I want to check list is containing the values on the column or not

list_of_value=['AB','AC','AD','AE']

Value     AB      AC     AD      AE  Dummy 
AB,AE     True  False  False   True    2
AE,RF,AW  False False  False   True    2 
AC,AD     False True   True    False   2

Tried with

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['Value'].isin(list_of_value))

Not getting the desired output

>Solution :

Use df['Value'].str.get_dummies:

df[list_of_value] = df['Value'].str.get_dummies(sep=',')[list_of_value].astype(bool)

Output:

>>> df
      Value  Dummy     AB     AC     AD     AE
0     AB,AE      2   True  False  False   True
1  AE,RF,AW      2  False  False  False   True
2     AC,AD      2  False   True   True  False
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