在大 pandas 分组之后绘制多个时间序列 [英] Plotting multiple time series after a groupby in pandas

查看:49
本文介绍了在大 pandas 分组之后绘制多个时间序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在valgdata DataFrame上进行了分组,如下所示:

Suppose I made a groupby on the valgdata DataFrame like below:

grouped_valgdata = valgdata.groupby(['news_site','dato_uden_tid']).mean()

现在我明白了:

                                  sentiment
news_site          dato_uden_tid           
dr.dk              2015-06-15     54.777183
                   2015-06-16     54.703167
                   2015-06-17     54.948775
                   2015-06-18     54.424881
                   2015-06-19     53.290554
eb.dk              2015-06-15     53.279251
                   2015-06-16     53.285643
                   2015-06-17     53.558753
                   2015-06-18     52.854750
                   2015-06-19     54.415988
jp.dk              2015-06-15     56.590428
                   2015-06-16     55.313752
                   2015-06-17     53.771377
                   2015-06-18     53.218408
                   2015-06-19     54.392638
pol.dk             2015-06-15     54.759532
                   2015-06-16     55.182641
                   2015-06-17     55.001800
                   2015-06-18     56.004326
                   2015-06-19     54.649052

现在,我想为每个news_site建立一个时间序列,其中dato_uden_tid在X轴上,而情感在Y轴上.

Now I want to make a timeseries for each of the news_site, where dato_uden_tid is on the X axis and sentiment is on Y axis.

实现此目标的最佳和最简便的方法是什么?

What is the best and easiest way to accomplish that?

谢谢!

推荐答案

(有点逗乐,因为这个问题使我做了同样的事情.)

(Am a bit amused, as this question caught me doing the exact same thing.)

您可以做类似的事情

valgdata\
    .groupby([valgdata.dato_uden_tid.name, valgdata.news_site.name])\
    .mean()\
    .unstack()

将会

  • 反转分组依据

  • reverse the groupby

将新网站堆叠为列

要进行绘制,只需立即执行上一个代码片段,然后紧跟.plot():

To plot, just do the previous snippet immediately followed by .plot():

valgdata\
    .groupby([valgdata.dato_uden_tid.name, valgdata.news_site.name])\
    .mean()\
    .unstack()\
    .plot()

这篇关于在大 pandas 分组之后绘制多个时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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