pandas 和总和与总和在同一数据框中 [英] Pandas and sum and cum sum in same dataframe

查看:60
本文介绍了 pandas 和总和与总和在同一数据框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的方法创建一个总和和一个总和.但是它们在两个单独的数据帧中.我要合而为一

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

     asqsum cumsum
asp     
0     20      20
1     20      40

推荐答案

也许您想要这个?

In [20]: df['asq_cum']=df['asq'].cumsum()

In [21]: df
Out[21]: 
     asq  asq_cum
asp              
0     20       20
1     20       40

这篇关于 pandas 和总和与总和在同一数据框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆