6
$\begingroup$

I have a datset with Scores and Categories and I would like to calculate the summary statistics for each of these categories. 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 would be something like this

Category    Count   Mean   Std   Min   25%   50%   75%   Max     
AAAA        
AAAA        
AAAA        
BBBB        
BBBB        
BBBB        
CCCC        
CCCC        

I have been looking at using pandas with a combination of both .groupby() and .describe() like this

df.groupby('Category')['Score'].describe()

and this almost looks like what I want but when I come to view this as a Dataset, all of the stats are in the index. I would like the data to be in the form of a table so I can output it and create a visualization off of the back of it.

Any ideas?

Thanks

$\endgroup$
  • $\begingroup$ Could you clarify your question? It's not very clear what you are trying to achieve... What is wrong with df.groupby('Category')['Score'].describe()? $\endgroup$ – MaxU Aug 15 '17 at 12:05
  • $\begingroup$ Apologies @MaxU. Hopefully I have clarified now :-) $\endgroup$ – Taylrl Aug 15 '17 at 12:31
14
$\begingroup$

IIUC:

In [80]: df.groupby("Category")['Score'].describe().reset_index()
Out[80]:
  Category  count       mean        std   min    25%    50%     75%    max
0     AAAA    3.0   1.666667   1.154701   1.0   1.00    1.0    2.00    3.0
1     BBBB    3.0  86.666667  79.839422   1.0  50.50  100.0  129.50  159.0
2     CCCC    2.0  -0.500000  13.435029 -10.0  -5.25   -0.5    4.25    9.0
| improve this answer | |
$\endgroup$

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.