Draw a cumulative chart from a pandas dataframe?

2024/9/25 14:24:41

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.

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!

Answer

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

https://en.xdnf.cn/q/71568.html

Related Q&A

Sort a complex Python dictionary by just one of its values

I am writing a little optimization tool for purchasing stamps at the post office.In the process I am using a dictionary, which I am sorting according to what I learned in this other "famous" …

How to modularize a Python application

Ive got a number of scripts that use common definitions. How do I split them in multiple files? Furthermore, the application can not be installed in any way in my scenario; it must be possible to have…

Solving for quartile and decile using Python

Is there a library for Python 2.7 that can solve quartiles and deciles. It seems that numpy doesnt have any functions for it. Can you give me a link if there are any. Thanks in advance! :D

Predicted values of each fold in K-Fold Cross Validation in sklearn

I have performed 10-fold cross validation on a dataset that I have using python sklearn, result = cross_val_score(best_svr, X, y, cv=10, scoring=r2) print(result.mean())I have been able to get the mean…

What does the comma mean in Pythons unpack?

We can simply use: crc = struct.unpack(>i, data)why do people write it like this: (crc,) = struct.unpack(>i, data)What does the comma mean?

How do I manually add more cookies to a session which already has cookies set in mechanize?

I have a python script which scrapes a page and receives a cookie. I want to append another cookie to the existing cookies that are being send to the server. So that on the next request I have the cook…

python generators time complexity confusion

I have been reading about keyword yield and generators in python and I want to know if I have understood it right it terms of time complexity.Here is my generator function to get factors:def calc_facto…

Python: Find Amount of Handwriting in Video

Do you know of an algorithm that can see that there is handwriting on an image? I am not interested in knowing what the handwriting says, but only that there is one present? I have a video of someone…

Include nonce and block count in PyCrypto AES MODE_CTR

Some background information, you can skip this part for the actual questionthis is my third question about this topic here at stackoverflow. To be complete, these are the other questions AES with crypt…

Why is pandas.series.map so shockingly slow?

Some days I just hate using middleware. Take this for example: Id like to have a lookup table that maps values from a set of inputs (domain) values, to outputs (range) values. The mapping is unique. A …