user2100039
user2100039

Reputation: 1356

Pandas Calculation Grouped By Unique Rows of Column Value

I am trying to calculate the standard deviation of column below "Pos Strikes" for the 3 year periods i have in the data below by "Plant" so that I have the result of "Plant" and the standard deviation for the 3 year period. My data looks like this:

         Plant       year           Pos Strikes
0        A           2018           38
1        A           2019            6
2        A           2020           33
3        B           2018           12
4        B           2019           30
5        B           2020           10

The end result should look like this:

         Plant       Pos Strikes Std Dev
0        A           17
1        B           11

I have tried this

ypos.groupby(['Plant','year'])[["Pos Strikes"]].std().reset_index().rename_axis(None, axis=1)

but I get NaN for each year that looks like this:

              Plant  year  Pos Strikes
0                 A  2018          NaN
1                 A  2019          NaN
2                 A  2020          NaN

Thank you for any help with this!

Upvotes: 1

Views: 19

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150735

I believe you want to group on Plant only:

df.groupby('Plant')['Pos Strikes'].std()

Output:

Plant
A    17.214335
B    11.015141
Name: Pos Strikes, dtype: float64

Upvotes: 2

Related Questions