简体   繁体   中英

How to calculate cumulative sum in python using pandas of all the columns except the first one that contain names?

Here's the data in csv format:

Name 2012 2013 2014 2015 2016 2017 2018 2019 2020
Jack   1   15   25    3    5   11   5    8    3
Jill   5   10   32    5    5   14   6    8    7

I don't want Name column to be include as it gives an error.

I tried

df.cumsum()

Try with set_index and reset_index to keep the name column:

df.set_index('Name').cumsum().reset_index()

Output:

   Name  2012  2013  2014  2015  2016  2017  2018  2019  2020
0  Jack     1    15    25     3     5    11     5     8     3
1  Jill     6    25    57     8    10    25    11    16    10

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