[英]Pandas DF - Group by + Sum and then Draw Lines for each Category Totals
我有一個帶有縣和時間列的氣體消耗數據的 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() 方法,但我無法進一步進行。
IIUC,嘗試:
df[['Gas','ReportDate_Time', 'County']].groupby(['ReportDate_Time', 'County'])\
.sum()\
.unstack()\
.plot()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.