pandas 爆炸成行 [英] Pandas explode column into rows

查看:71
本文介绍了 pandas 爆炸成行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataFrame,其中每一行都有两列:日期和提及.最终结果将是每个日期的提及数据框,如果我可以打破提及,这将很容易通过GroupBy进行,这就是我遇到的问题.原始数据如下:

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

我需要转换为此:

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

最终状态应如下所示,我可以通过GroupBy的值计数(加上重新索引编制)来获得该结果:

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.

推荐答案

如果最终结果是虚拟列,请使用pd.Series.str.get_dummies

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


@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

这篇关于 pandas 爆炸成行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆