0

I have a dataframe called 'data' that keeps getting appended to over time and then the AVG/STD gets recalculated and added on after (in the future it may be MONDAY, TUESDAY WEDNESDAY, THURSDAY, AVERAGE,STD_DEV).

ID    MONDAY   TUESDAY    AVERAGE  STD_DEV
 a         1         2        ...      ...
 b         1         2        ...      ...
 c         1         2        ...      ...

I want to have columns dedicated to the sum of its counterpart for each column all the way through.

ID    MONDAY   TUESDAY    AVERAGE  STD_DEV     MONDAY_SUM   TUESDAY_SUM
 a         1         2        ...      ...              3             6
 b         1         2        ...      ...
 c         1         2        ...      ...

I already drop everything from AVERAGE column and on when first reading in the data so I can add on extra days then recalculate AVG,STD. How would I write code to get a sum column appended for each column between ID and AVERAGE for a growing/shrinking dataframe?

1 Answer 1

1

Let us try

df = df.join(df.loc[:,'ID':'AVERAGE'].iloc[:,1:-1].sum().to_frame().T.add_suffix('_sum'))
Out[206]: 
  ID  MONDAY  TUESDAY AVERAGE STD_DEV  MONDAY_sum  TUESDAY_sum
0  a       1        2     ...     ...         3.0          6.0
1  b       1        2     ...     ...         NaN          NaN
2  c       1        2     ...     ...         NaN          NaN
Sign up to request clarification or add additional context in comments.

2 Comments

thanks! by the way, how would I divide all the new sums by some scalar? im trying to include a .div call in the one liner but it's getting very messy @BEN_YO
@stackoverflow I am not sure what you need ~ maybe you can open a new question ?

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.