Tampa
Tampa

Reputation: 78254

Pandas and sum and cum sum in same dataframe

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

Upvotes: 6

Views: 14130

Answers (1)

Nipun Batra
Nipun Batra

Reputation: 11367

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

Upvotes: 16

Related Questions