Giuseppe Minardi
Giuseppe Minardi

Reputation: 411

From column of lists to multiple columns

I have a column formatted like this:

American express,Visa,Master Card,Diners
American express,Visa,Master Card,Bancomat
all
Visa,Master Card,Diners,Banco

I want to create different columns for each element with 1 or 0 if the given card is found in the list or not.

The expected results should be something like this:

Visa    American Express    Master Card   Diners    Bancomat
1       1                   1             1         0
1       1                   1             0         1
1       1                   1             1         1
1       0                   1             1         0

Is there a way in pandas to do so?

Upvotes: 1

Views: 43

Answers (1)

jezrael
jezrael

Reputation: 862641

Use Series.str.get_dummies with processing all column by DataFrame.add with DataFrame.pop for extract column:

print (df)
                                          col
0    American express,Visa,Master Card,Diners
1  American express,Visa,Master Card,Bancomat
2                                         all
3            Visa,Master Card,Diners,Bancomat

df = df['col'].str.get_dummies(',')

df = df.add(df.pop('all'), axis=0)
#alternative
#df += df.pop('all')[:, None]
print (df)
   American express  Bancomat  Diners  Master Card  Visa
0                 1         0       1            1     1
1                 1         1       0            1     1
2                 1         1       1            1     1
3                 0         1       1            1     1

Upvotes: 2

Related Questions