# Calculating the Standard Deviation by category using Python

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:

numpy.std(a)


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

• I highly recommend you to use pandas in these types of work, as the answer suggested. Aug 14 '17 at 11:23
• This one should be moved to stack-overflow. There's no science stuff here. 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)

• Amazing. Great answer! Aug 14 '17 at 13:33
• I think we can get rid of .apply() - df.groupby("Category").std(ddof=0)
– 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.

• 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 Nov 25 '19 at 15:01