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?
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.
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.
This is cumsum:
df['X'] = df['C'].cumsum()
df['Y'] = df['C'].sum() + df['C'] - df['X']
# or
# df['Y'] = df.iloc[::-1].cumsum()
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With