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

编程入门 行业动态 更新时间:2024-10-20 01:22:17
本文介绍了从 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 数据框绘制累积图表?

本文发布于:2023-11-22 14:57:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1617883.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图表   数据   pandas

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!