Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sum column above/below current row in pandas

I'm trying to achieve this in pandas:

df['X'] = df['C'].where(row is <= current row).sum()
df['Y'] = df['C'].where(row is >= current row).sum()

What is the right syntax to make pandas sum the data from column C that are above or equal to the current row?

like image 545
Brannon Avatar asked Oct 08 '20 14:10

Brannon


People also ask

How do I sum specific columns in pandas?

To sum given or list of columns then create a list with all columns you wanted and slice the DataFrame with the selected list of columns and use the sum() function. Use df['Sum']=df[col_list]. sum(axis=1) to get the total sum.

How do I get the sum of multiple columns in pandas?

Sum all columns in a Pandas DataFrame into new column If we want to summarize all the columns, then we can simply use the DataFrame sum() method.


2 Answers

This is cumsum:

df['X'] = df['C'].cumsum()
df['Y'] = df['C'].sum() + df['C'] - df['X']
# or 
# df['Y'] = df.iloc[::-1].cumsum()
like image 87
Quang Hoang Avatar answered Oct 21 '22 15:10

Quang Hoang


Let us try with expanding you can chose the agg function you need etc, mean/std

df['X'] = df['C'].expanding().sum()
df['Y'] = df['C'].iloc[::-1].expanding().sum()
like image 2
BENY Avatar answered Oct 21 '22 16:10

BENY