MFranc
MFranc

Reputation: 396

Pandas : transform a column containing list of strings to new columns for each value

I'm trying to transform a column containing a list of values to a new set of columns, one for each value over all rows of that column.

For example given :

index    cat
0       ['a','b']
1       ['c','a','d']
2       ['e','b','c']

I'd like to get :

index    a       b       c        d         e
0        1       1       0        0         0
1        1       0       1        1         0
2        0       1       1        0         1

Could you help me and point me in the right direction?Thanks

Upvotes: 1

Views: 40

Answers (1)

ansev
ansev

Reputation: 30920

Use:

#df=df.set_index('index') #if index is a column
d=df.explode('cat')
new_df=pd.crosstab(d.index,d.cat)
print(new_df)

Output

cat    a  b  c  d  e
row_0               
0      1  1  0  0  0
1      1  0  1  1  0
2      0  1  1  0  1

print(df)
         cat
0     [a, b]
1  [c, a, d]
2  [e, b, c]

Upvotes: 3

Related Questions