Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split pandas column and create new columns that count the split values

Tags:

python

pandas

I have a goofy data where one column contains multiple values slammed together with a comma:

In [62]: df = pd.DataFrame({'U': ['foo', 'bar', 'baz'], 'V': ['a,b,a,c,d', 'a,b,c', 'd,e']})                                     

In [63]: df                                                                                                                      
Out[63]: 
     U          V
0  foo  a,b,a,c,d
1  bar      a,b,c
2  baz        d,e

Now I want to split column V, drop it, and add columns a through e. Columns a through e should contains the count of the occurrences of that letter in that row:

In [62]: df = pd.DataFrame({'U': ['foo', 'bar', 'baz'], 'V': ['a,b,a,c,d', 'a,b,c', 'd,e']})                                     

In [63]: df                                                                                                                      
Out[63]: 
     U  a  b  c  d  e
0  foo  2  1  1  1  0
1  bar  1  1  1  0  0
2  baz  0  0  0  1  1

Maybe some combination of df['V'].str.split(',') and pandas.get_dummies but I can't quite work it out.

Edit: apparently I have to justify why my question is not a duplicate. I think why is intuitively obvious to the most casual observer.

like image 420
Paul Coccoli Avatar asked Feb 26 '26 22:02

Paul Coccoli


2 Answers

This is str.get_dummies

pd.concat([df,df.pop('V').str.split(',',expand=True).stack().str.get_dummies().sum(level=0)],1)
Out[602]: 
     U  a  b  c  d  e
0  foo  2  1  1  1  0
1  bar  1  1  1  0  0
2  baz  0  0  0  1  1
like image 135
BENY Avatar answered Feb 28 '26 12:02

BENY


You could just use pandas.Series.str.count. For example:

import pandas as pd

df = pd.DataFrame({'U': ['foo', 'bar', 'baz'], 'V': ['a,b,a,c,d', 'a,b,c', 'd,e']})

columns = ['a', 'b', 'c', 'd', 'e']
# If unknown or large set of columns, then replace above with:
# columns = sorted(set(df['V'].str.split(',').sum()))

for column in columns:
    df[column] = df['V'].str.count(column)

print(df)
#      U          V  a  b  c  d  e
# 0  foo  a,b,a,c,d  2  1  1  1  0
# 1  bar      a,b,c  1  1  1  0  0
# 2  baz        d,e  0  0  0  1  1
like image 38
benvc Avatar answered Feb 28 '26 10:02

benvc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!