运行 总共 pandas df
Running total on a pandas df
我正在尝试将 运行 count
添加到 pandas df
。
对于 Column A
中的值,我想添加 '5'
,对于 Column B
中的值,我想添加 '1'
.
所以对于下面的 df
我希望产生:
A B Total
0 0 0 0
1 0 0 0
2 1 0 5
3 1 1 6
4 1 1 6
5 2 1 11
6 2 2 12
因此,对于 Column A
中的每个增量 integer
,总计等于 '5'
。而 Column B
是 '+1'
.
我试过了:
df['Total'] = df['A'].cumsum(axis = 0)
但这不包括 B 列
df['Total'] = df['A'] * 5 + df['B']
据我所知,您只是在进行逐行运算,而不是累加和。此代码段计算 A
的行值乘以 5,并为每一行添加 B
的行值。请不要让它比实际情况更复杂。
什么是累计和(也称为running total)?
来自维基百科:
Consider the sequence < 5 8 3 2 >. What is the total of this sequence?
Answer: 5 + 8 + 3 + 2 = 18. This is arrived at by simple summation of the sequence.
Now we insert the number 6 at the end of the sequence to get < 5 8 3 2 6 >. What is the total of that sequence?
Answer: 5 + 8 + 3 + 2 + 6 = 24. This is arrived at by simple summation of the sequence. But if we regarded 18 as the running total, we need only add 6 to 18 to get 24. So, 18 was, and 24 now is, the running total. In fact, we would not even need to know the sequence at all, but simply add 6 to 18 to get the new running total; as each new number is added, we get a new running total.
我正在尝试将 运行 count
添加到 pandas df
。
对于 Column A
中的值,我想添加 '5'
,对于 Column B
中的值,我想添加 '1'
.
所以对于下面的 df
我希望产生:
A B Total
0 0 0 0
1 0 0 0
2 1 0 5
3 1 1 6
4 1 1 6
5 2 1 11
6 2 2 12
因此,对于 Column A
中的每个增量 integer
,总计等于 '5'
。而 Column B
是 '+1'
.
我试过了:
df['Total'] = df['A'].cumsum(axis = 0)
但这不包括 B 列
df['Total'] = df['A'] * 5 + df['B']
据我所知,您只是在进行逐行运算,而不是累加和。此代码段计算 A
的行值乘以 5,并为每一行添加 B
的行值。请不要让它比实际情况更复杂。
什么是累计和(也称为running total)?
来自维基百科:
Consider the sequence < 5 8 3 2 >. What is the total of this sequence?
Answer: 5 + 8 + 3 + 2 = 18. This is arrived at by simple summation of the sequence.
Now we insert the number 6 at the end of the sequence to get < 5 8 3 2 6 >. What is the total of that sequence?
Answer: 5 + 8 + 3 + 2 + 6 = 24. This is arrived at by simple summation of the sequence. But if we regarded 18 as the running total, we need only add 6 to 18 to get 24. So, 18 was, and 24 now is, the running total. In fact, we would not even need to know the sequence at all, but simply add 6 to 18 to get the new running total; as each new number is added, we get a new running total.