6

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?

0

2 Answers 2

6

This is cumsum:

df['X'] = df['C'].cumsum()
df['Y'] = df['C'].sum() + df['C'] - df['X']
# or 
# df['Y'] = df.iloc[::-1].cumsum()
Sign up to request clarification or add additional context in comments.

3 Comments

very needed question and answer
follow up question: if I need to exclude the current row itself (aka, "row <" vs "row <="), is there an easy way to make cumsum do that?
@Brannon cumsum won't do, but combined with shift does: df['C'].shift().cumsum().
2

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.