简体   繁体   中英

How to add a new column that includes sum consecutive values of other column?

I am new to python. I want to create a column in pandas that includes the sum of consecutive values of a column. For example, let's assume we have the following table

id A
1 21
2 32
3 55

And I want to have this table

id A New column
1 21 21
2 32 53
3 55 108

Any help will be appreciated.

This is called a cumulative sum. You can just set df["C"] = df["A"].cumsum() .

https://pandas.pydata.org/docs/reference/api/pandas.Series.cumsum.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM