Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas explode column into rows

Tags:

python

pandas

I have a DataFrame where each row has two columns: date, and mentions. The end result would be a Dataframe of mentions per date, which should be easy via GroupBy if I can break out the mentions, which is where I am stuck. The original data looks like this:

date        mentions
2018-01-01  alpha, beta, gamma
2018-01-01  alpha
2018-01-02  beta
2018-01-03  delta
2018-01-05  alpha
2018-01-07  alpha
2018-01-10  delta, gamma
2018-01-11  gamma

Which I need to convert to this:

date        mentions
2018-01-01  alpha
2018-01-01  beta
2018-01-01  gamma
2018-01-01  alpha
2018-01-02  beta
2018-01-03  delta
2018-01-05  alpha
2018-01-07  alpha
2018-01-10  delta
2018-01-10  gamma
2018-01-11  gamma

And the end state should be like below, which I can get to by GroupBy value counts (plus reindexing):

date        alpha     beta     gamma     delta
2018-01-01  2         1        1         0
2018-01-02  0         1        1         0
2018-01-03  0         0        0         1
2018-01-04  0         0        0         0
2018-01-05  1         0        0         0
2018-01-06  0         0        0         0
2018-01-07  1         0        0         0
2018-01-08  0         0        0         0
2018-01-09  0         0        0         0
2018-01-10  0         0        1         1
2018-01-11  0         0        1         0

I have seen variations on this problem elsewhere, but not quite like mine, which I feel is very simple and I'm just not seeing the right solution.

like image 827
whateveryousayiam Avatar asked Sep 08 '26 04:09

whateveryousayiam


1 Answers

If your end result is dummy columns then use pd.Series.str.get_dummies

df.set_index('date').mentions.str.get_dummies(', ').sum(level=0)

            alpha  beta  delta  gamma
date                                 
2018-01-01      2     1      0      1
2018-01-02      0     1      0      0
2018-01-03      0     0      1      0
2018-01-05      1     0      0      0
2018-01-07      1     0      0      0
2018-01-10      0     0      1      1
2018-01-11      0     0      0      1

As mentioned by @Zero

df.set_index('date').mentions.str.get_dummies(', ').resample('D').sum()
            alpha  beta  delta  gamma
date                                 
2018-01-01      2     1      0      1
2018-01-02      0     1      0      0
2018-01-03      0     0      1      0
2018-01-04      0     0      0      0
2018-01-05      1     0      0      0
2018-01-06      0     0      0      0
2018-01-07      1     0      0      0
2018-01-08      0     0      0      0
2018-01-09      0     0      0      0
2018-01-10      0     0      1      1
2018-01-11      0     0      0      1
like image 133
piRSquared Avatar answered Sep 10 '26 18:09

piRSquared



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!