简体   繁体   English

熊猫:cumsum忽略前两行

[英]pandas: cumsum ignoring first two rows

I have a dataframe which has the following column: 我有一个包含以下列的数据框:

|---------------------| 
|          A          |
|---------------------|
|           0         |
|---------------------|
|         2.63        |
|---------------------|
|         7.10        |
|---------------------|
|         5.70        |
|---------------------|
|         6.96        |
|---------------------|
|         7.58        |
|---------------------|
|         3.3         |
|---------------------|
|         1.93        |
|---------------------|

I need to get the cumulative sum, but the point is kind of particular. 我需要获得累计和,但是要点很特别。 The first element should be 0, and the following are the cumulative sum starting from the previous column, so in this case I need to produce: 第一个元素应为0,以下是从上一列开始的累积总和,因此在这种情况下,我需要产生:

|---------------------| 
|          B          |
|---------------------|
|           0         |
|---------------------|
|           0         |
|---------------------|
|         2.63        |
|---------------------|
|         9.73        |
|---------------------|
|        15.43        |
|---------------------|
|        22.39        |
|---------------------|
|        29.97        |
|---------------------|
|        33.27        |
|---------------------|

I know that it is easily achieve when not having the condition I am asking for by: 我知道,如果不满足以下条件,则很容易实现:

df['B'] = df.A.cumsum()

However, I don't have any idea how to solve this issue, and I was thinking to implement a for loop, but I hope there is a simply way using pandas. 但是,我不知道如何解决此问题,我当时正在考虑实现for循环,但是我希望有一种简单的方法可以使用熊猫。

You can add shift and fillna : 您可以添加shiftfillna

df = df.A.cumsum().shift().fillna(0)
print (df)
0     0.00
1     0.00
2     2.63
3     9.73
4    15.43
5    22.39
6    29.97
7    33.27
Name: A, dtype: float64

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

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