pandas 从熊猫数据框中绘制累积图表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37293014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:15:11  来源:igfitidea点击:

Draw a cumulative chart from a pandas dataframe?

pythonpandasmatplotlib

提问by Richard

I have a dataframe as follows:

我有一个数据框如下:

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

How can I draw a cumulative chart of the savings?

如何绘制储蓄累积图表?

I'm thinking of a line chart with number of items on the x-axis, and total savings on the y-axis.

我正在考虑一个折线图,x 轴上有项目数,y 轴上有总储蓄。

The chart should show that the bulk of the savings come from a few items.

该图表应显示大部分节省来自少数项目。

I've tried:

我试过了:

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

but it doesn't plot the cumulative values.

但它没有绘制累积值。

Thanks for any help!

谢谢你的帮助!

回答by piRSquared

I did:

我做了:

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

And got:

并得到:

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

This:

这个:

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

Gets me:

得到我:

enter image description here

在此处输入图片说明