Map values from a list of values in one of the dataframe columns

65 views Asked by At

the following situation: enter image description here

Equipment is a pandas series and far as I know (split response said so). What I like to do is: If the value ABS shows up, insert into the corresponding column ABS 1, if the value is in Equipment or 0, the value is not in equipment - or copy ABS into that corresponding field and if not, leave it as it is.

I used something similiar, but it doesn't work well:

df_copy.loc[df_copy['col2']=='str1', 'col4'] = df_copy['col3']

The next thing is, I have over 90 values/columns so doing this for all would be very time consuming.

Can someone help me out?

Thanks and have a nice one.

1

There are 1 answers

12
gtomer On

Is this what you have in mind?:

import pandas as pd
df = pd.DataFrame({'equipment': {'0': ['ABS',  'ABS' , 'Android Auto'],
                              '1': ['ABS', 'abstand']}})
df = pd.get_dummies(df.equipment.explode()).groupby(level=0).agg(sum)
df

   ABS  Android Auto  abstand
0    2             1        0
1    1             0        1