熊猫DF-按+总和分组,然后为每个类别总计绘制线条

时间:2020-01-22 21:55:57

标签: python pandas matplotlib

我有一个带有燃气消耗数据的DF,带有县和时间列。

df[['Gas','ReportDate_Time', 'County']].head()


Gas ReportDate_Time County
0   3757.0  2017-10-01  MCK
1   2271.0  2017-10-01  MCK
2   2366.0  2017-10-01  MCK
3   1749.0  2017-10-01  MCK
4   1173.0  2017-10-01  MCK

我按县和日期分组,并对结果求和:

df[['Gas','ReportDate_Time', 'County']].groupby(['ReportDate_Time', 'County']).sum().reset_index()

    ReportDate_Time County  Gas
0   2015-05-01  BIL 543126.0
1   2015-05-01  BOT 26681.0
2   2015-05-01  BOW 1028693.0
3   2015-05-01  BRK 754380.0
4   2015-05-01  DIV 1494449.0
... ... ... ...
533 2018-02-01  REN 2720.0
534 2018-02-01  SLP 6656.0
535 2018-02-01  STK 570531.0
536 2018-02-01  WIL 10085655.0
537 2018-02-01  WRD 1167.0

我希望有一个带有多条线的图,每条线代表跨时间的Gas。我尝试以几种不同的方式使用.plot()方法,但无法继续进行操作。

1 个答案:

答案 0 :(得分:1)

IIUC,请尝试:

df[['Gas','ReportDate_Time', 'County']].groupby(['ReportDate_Time', 'County'])\
                                       .sum()\
                                       .unstack()\
                                       .plot()
相关问题