Back to Community
Need help with calculating sector specific z-score

I have the following code for calculating z-score that works

def get_zscore(fundamental_df,columns):  
    numeric_df = fundamental_df.loc[columns]  
    means = numeric_df.mean(axis=1)  # mean across securities  
    sds = numeric_df.std(axis=1)  # SDs across securities  
    de_meaned = numeric_df.sub(means, axis=0)  
    return de_meaned.div(sds, axis=0)

myzscore = get_zscore(fundamental_df,[['ev_to_ebitda']])  

I am trying to figure out what the simplest way to do this on per sector basis??

preferably something that does not involve looping through sectors.

3 responses

Aite found a soluton.
Documenting it here under
"Calculate Z-Score for specific fundamental fields in the Quantopian fundamentals DataFrame, but do it group-wise by Morning Start Sector Code"

Can someone more familiar with pandas please correct me if I am wrong.

Use the pandas groupby function; groupby sector code.

Yeah, you will want to use GroupBy.