Python 熊猫DF——分组依据+;求和,然后为每个类别总计画线

Python 熊猫DF——分组依据+;求和,然后为每个类别总计画线,python,pandas,matplotlib,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

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
我想有一个有多条线的单一绘图,每条线代表一段时间内的气体。我尝试过以几种不同的方式使用.plot()方法,但无法继续进行。

IIUC,请尝试:

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

你想要一个条形图还是线形图?两者都可以,我想我应该使用.unstack()方法,但是继续使用这个!我想你可能想要一个测线图,因为你正试图绘制一个显示一个县随时间变化的天然气趋势的图。每个县都将是您的线路,然后值将随时间而变化。条形图不能真的做到这一点。。。