Home > Mobile >  How to check data inside column pandas
How to check data inside column pandas

Time:06-01

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

df=(df['Value'].isin(list_of_value))

Not getting the desired output

CodePudding user response:

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
  • Related