简体   繁体   English

如何使用除第一个包含名称的列之外的所有列的 pandas 计算 python 中的累积总和?

[英]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:以下是 csv 格式的数据:

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:尝试使用set_indexreset_index来保留名称列:

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

Output: 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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