Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas and sum and cum sum in same dataframe

Tags:

python

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
like image 851
Tampa Avatar asked Jan 01 '14 11:01

Tampa


1 Answers

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
like image 132
Nipun Batra Avatar answered Oct 17 '22 22:10

Nipun Batra