简体   繁体   English

Pandas 计算按列值的唯一行分组

[英]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.

相关问题 过滤分组的熊猫数据框,保留列中具有最小值的所有行 - Filter grouped pandas dataframe, keep all rows with minimum value in column 如何获取具有唯一列值的行数(按其他列值分组)? - How to get number of rows with a unique column value (grouped by an other column value)? Python PANDAS:新建列,将唯一值应用于所有行 - Python PANDAS: New Column, Apply Unique Value To All Rows 从数据框中获取列中唯一值的最后一行 - Pandas - Get the last rows for a unique value in a column from a dataframe - Pandas 使用 pandas 过滤列中具有唯一值的行 - Filtering rows that have unique value in a column using pandas 熊猫:即使缺少数据,也为列的每个唯一值创建行 - Pandas: create rows for each unique value of a column, even with missing data 如何为pandas数据框中按ID分组的每一列找到每个唯一值的最小值 - How to find minimum for each unique value for every column grouped by ID in pandas data frame 根据计算扩展 Pandas 列以分隔行 - Expanding Pandas Column to Separate Rows based on calculation 删除按字符串值分组的行 - pandas - Drop rows grouped by string value - pandas 在 pandas dataframe 中,如何根据列值过滤行,进行计算并将结果分配给新列? - In a pandas dataframe, how can I filter the rows based on a column value, do calculation and assign the result to a new column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM