1

I just started learning Python so forgive me if this question has already been answered somewhere else. I want to create a new column called "Sum", which will simply be the previous columns added up.

Risk_Parity.tail()

    VCIT  VCLT  PCY     RWR     IJR     XLU     EWL
Date                            
2017-01-31  21.704155   11.733716   9.588649    8.278629    5.061788    7.010918    7.951747
2017-02-28  19.839319   10.748690   9.582891    7.548530    5.066478    7.453951    7.950232
2017-03-31  19.986782   10.754507   9.593623    7.370828    5.024079    7.402774    7.654366
2017-04-30  18.897307   11.102380   10.021139   9.666693    5.901137    7.398604    11.284331
2017-05-31  63.962659   23.670240   46.018698   9.917160    15.234977   12.344524   20.405587

The table columns are a little off but all I need is (21.70 + 11.73...+7.95) I can only get as far as creating the column Risk_Parity['sum'] = , but then I'm lost.

I'd rather not do have to do Risk_Parity['sum] = Risk_Parity['VCIT'] + Risk_Parity['VCLT']...

After creating the sum column, I want to divide each column by the sum column and make that into a new dataframe, which wouldn't include the sum column.

If anyone could help, I'd greatly appreciate it. Please try to dumb your answers down as much as possible lol.

Thanks!

Tom

1
  • Have you tried Risk_Parity.sum(1)? Commented Jun 12, 2017 at 16:03

2 Answers 2

5

Use sum with the parameter axis=1 to specify summation over rows

Risk_Parity['Sum'] = Risk_Parity.sum(1)

To create a new copy of Risk_Parity without writing a new column to the original

Risk_Parity.assign(Sum= Risk_Parity.sum(1))

Notice also, that I named the column Sum and not sum. I did this to avoid colliding with the very same method named sum I used to create the column.


To only include numeric columns... however, sum knows to skip non-numeric columns anyway.

RiskParity.assign(Sum=RiskParity.select_dtypes(['number']).sum(1))
# same as
# RiskParity.assign(Sum=RiskParity.sum(1))

             VCIT   VCLT    PCY   RWR    IJR    XLU    EWL     Sum
Date                                                              
2017-01-31  21.70  11.73   9.59  8.28   5.06   7.01   7.95   71.33
2017-02-28  19.84  10.75   9.58  7.55   5.07   7.45   7.95   68.19
2017-03-31  19.99  10.75   9.59  7.37   5.02   7.40   7.65   67.79
2017-04-30  18.90  11.10  10.02  9.67   5.90   7.40  11.28   74.27
2017-05-31  63.96  23.67  46.02  9.92  15.23  12.34  20.41  191.55
Sign up to request clarification or add additional context in comments.

2 Comments

will this know not to include the Date column or any potential character columns? Just out of interest, I'm not that familiar with pandas.
The Date column is not a column, it's in the index. So yes. As for the non-character columns... I'll update my post.
0
l = ['VCIT' , VCLT' ,PCY' ... 'EWL']
Risk_Parity['sum'] = 0
for item in l:
    Risk_Parity['sum'] += Risk_Parity[item]

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.