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

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

问题描述

假设我在 valgdata DataFrame 上创建了一个 groupby,如下所示:

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 中的 groupby 之后绘制多个时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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