Lonewolf
Lonewolf

Reputation: 197

Getting an error when calculating standard deviation using Pandas

I am trying to calculate standard deviation of multiple columns using two variables in the groupby. However, my code throws in an error and I am having a hard time figuring it out.

I am using https://www.shanelynn.ie/summarising-aggregation-and-grouping-data-in-python-pandas/ as a guide.

Below is a sample dataframe:

Book Home    Num    Pointspread  odds
A   P       -135    -2.5        -110.0
B   P        NaN    -3          -101.0
B   P        NaN    -3          -110.0
C   P        NaN    -3          -120.0
B   P        NaN    -3          -100.0

and this the the code I wrote

home_std_dev = home_analysis_data.groupby('Book','Home').agg({'Num':'std',
                                                          'Pointspread':'std',
                                                           'odds':'std'})

The code above gives me an error

ValueError: No axis named Home for object type <class 'type'>

I don't know what this error means and how to solve the issue. I am expecting to see a table with the standard deviation of the columns grouped by the two variables. Any help will be appreciated.

Since I'm quite new to python, please let me know if there is a better way to approach this issue. Thank you!

Upvotes: 1

Views: 178

Answers (1)

jezrael
jezrael

Reputation: 863651

Use list in groupby - ['Book','Home'] for grouping by multiple columns:

home_std_dev = home_analysis_data.groupby(['Book','Home']).agg({'Num':'std',
                                                      'Pointspread':'std',
                                                       'odds':'std'})

Upvotes: 4

Related Questions