I have a datset with Scores and Categories and I would like to calculate the Standard Deviation of these scores, per category. The data look something like this:

Category    Score    
AAAA        1
AAAA        3
AAAA        1
BBBB        1
BBBB        100
BBBB        159
CCCC        -10
CCCC        9

What I would then like is the Standard Deviation of each Category. I know that with numpy I can use the following:


But the example I can find only have this relating to a list and not a range of different categories in a DataFame.

  • $\begingroup$ I highly recommend you to use pandas in these types of work, as the answer suggested. $\endgroup$ – Blaszard Aug 14 '17 at 11:23
  • $\begingroup$ This one should be moved to stack-overflow. There's no science stuff here. $\endgroup$ – Piotr Rarus - Reinstate Monica Nov 25 '19 at 14:38

You can easily do this using pandas:

import pandas as pd
import numpy as np

df = pd.DataFrame([["AA", 1], ["AA", 3], ["BB", 3], ["CC", 5], ["BB", 2], ["AA", -1]])
df.columns = ["Category", "Score"]
print df.groupby("Category").apply(np.std)
| improve this answer | |
  • $\begingroup$ Amazing. Great answer! $\endgroup$ – Taylrl Aug 14 '17 at 13:33
  • 1
    $\begingroup$ I think we can get rid of .apply() - df.groupby("Category").std(ddof=0) $\endgroup$ – MaxU Aug 15 '17 at 12:08

I have a slight variation in the input data. I have more than one column, so how to give command to pick a specific column for the calculation of std deviation.

| improve this answer | |
  • $\begingroup$ adapting @MaxU's comment, you can do column access on the groupby object as if it was a dataframe. So df.groupby('Category')['specific_column'].std(ddof=0) would work $\endgroup$ – Tom M. Nov 25 '19 at 15:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.