Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Help me in performing calculations on a pandas dataframe

Hi!
I need help in performing some calculations on a pandas dataframe.
MKT caps are fictional, also the factor all_categories_momentum_daily is fictional, I didn't bother giving the right numbers...

Given the attached notebook, I'm at the last cell and have df2, already divided in quantiles (quintiles in this case).

For now, I would like to add a new column, for each stock, called "weighted factor".
For every stock, in order to obtain this value, I need to divide each stock's mkt cap by the sum of the mkt cap of every quintile multiplied by each stock's all_categories_momentum_daily factor

3 responses

For instance for AMZN this value "weighted factor" would be :
699900/( 699900+3000000) * 6.88800

This should do the trick. I tried to explain why you need one pandas method, transform('sum'), as opposed to just .sum() after using groupby().

@Kyle, thank you so much!
I did indeed try to use the code with .sum but it didn't work, now I understand why.