从 pandas 数据框绘制累积图表? [英] Draw a cumulative chart from a pandas dataframe?

查看:64
本文介绍了从 pandas 数据框绘制累积图表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,如下所示:

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?

我正在考虑一个折线图,在x轴上具有项目数量,在y轴上具有总节省量.

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

图表应显示节省的大部分资金来自一些项目.

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

我尝试过:

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

,但不绘制累积值.

感谢您的帮助!

推荐答案

我做到了:

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')

得到我:

这篇关于从 pandas 数据框绘制累积图表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆