Python Pandas calucate Z score of groupby means
Question: I have a dataframe like this:

    
    
    df = pd.DataFrame({'Year' : ['2010', '2010', '2010', '2010', '2010', '2011', '2011', '2011', '2011', '2011', '2012', '2012', '2012', '2012', '2012'],
                       'Name' : ['Bob', 'Joe', 'Bill', 'Bob', 'Joe', 'Dave', 'Bob', 'Joe', 'Bill', 'Bill', 'Joe', 'Dave', 'Dave', 'Joe', 'Steve'],
                        'Score' : [95, 76, 77, 85, 82, 92, 67, 80, 77, 79, 82, 92, 64, 71, 83]})
    

I would like to get the Z Score for each _Name_ in each _Year_.

I can do it if subset the Year column like this:

    
    
    (df[df.Year == '2010'].groupby(['Year', 'Name'])['Score'].mean() - df[df.Year == '2010'].groupby(['Year', 'Name'])['Score'].mean().mean()) / ( df[df.Year == '2010'].groupby(['Year', 'Name'])['Score'].mean().std())
    

Is there a cleaner way of doing it?


Answer: There is a `zscore` functionality in `scipy`, but be careful the default
delta-degree-of-freedom is 0 in `scipy.stats.zscore`:

    
    
    In [171]:
    import scipy.stats as ss
    S=(df[df.Year == '2010'].groupby(['Year', 'Name'])['Score'].mean())
    pd.Series(ss.zscore(s, ddof=1), S.index)
    Out[171]:
    Year  Name
    2010  Bill   -0.714286
          Bob     1.142857
          Joe    -0.428571
    dtype: float64
    

