[英]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.我正在尝试通过“Plant”计算我在下面数据中的 3 年期间“Pos Strikes”下方列的标准偏差,以便我得到“Plant”的结果和 3 年期间的标准偏差。 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:但我每年都会得到 NaN,如下所示:
Plant year Pos Strikes
0 A 2018 NaN
1 A 2019 NaN
2 A 2020 NaN
Thank you for any help with this!感谢您对此的任何帮助!
I believe you want to group on Plant
only:我相信您只想对Plant
进行分组:
df.groupby('Plant')['Pos Strikes'].std()
Output: Output:
Plant
A 17.214335
B 11.015141
Name: Pos Strikes, dtype: float64
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.