从Pandas数据框中绘制累积图表?

2024-10-01 11:38:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据框如下:

df = pd.DataFrame({'cost_saving': [10, 10, 20, 40, 60, 60],
                   'id': ['a', 'b', 'c', 'd', 'e', 'f']})

我怎样才能画出储蓄的累积图表?

我在想一个折线图,x轴上有项目数,y轴上有总节省。

图表应该显示大部分储蓄来自于一些项目。

我试过:

dftest['cost_saving'].plot(drawstyle='steps')

但它没有绘制累积值。

谢谢你的帮助!


Tags: 数据项目iddataframedfplot图表pd
1条回答
网友
1楼 · 发布于 2024-10-01 11:38:16

我做到了:

df.set_index('id').cumsum()

得到:

    cost_saving
id             
a            10
b            20
c            40
d            80
e           140
f           200

这:

df.reset_index().plot.line(df.cost_saving.cumsum(), 'index', drawstyle='steps')

让我明白:

enter image description here

相关问题 更多 >