Pandas 和 sum 和 cum sum 在同一数据框中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20868232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 21:30:17 来源:igfitidea点击:
Pandas and sum and cum sum in same dataframe
提问by Tampa
I use the below to create a sum and a cumsum. But they are in two separate dataframes. I want all in one
我使用下面的来创建一个总和和一个总和。但它们位于两个单独的数据帧中。我想要一应俱全
asp = np.array(np.array([0,0,1]))
asq = np.array(np.array([10,10,20]))
columns=['asp']
df = pd.DataFrame(asp, index=None, columns=columns)
df['asq'] = asq
df = df.groupby(by=['asp']).sum()
dfcum =df.cumsum()
How do I have both the sum and the cumsum in the same dataframe. Totally not clear how to do this. Below is what I want
我如何在同一个数据框中同时拥有 sum 和 cumsum。完全不清楚如何做到这一点。下面是我想要的
asqsum cumsum
asp
0 20 20
1 20 40
回答by Nipun Batra
Maybe you want this?
也许你想要这个?
In [20]: df['asq_cum']=df['asq'].cumsum()
In [21]: df
Out[21]:
asq asq_cum
asp
0 20 20
1 20 40

