【问题标题】:Pandas Calculation Grouped By Unique Rows of Column Value按列值的唯一行分组的 Pandas 计算
【发布时间】:2021-03-04 15:58:43
【问题描述】:

我正在尝试通过“Plant”计算我在下面数据中的 3 年期间“Pos Strikes”下方列的标准偏差,以便我得到“Plant”的结果和 3年期间。我的数据如下所示:

         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

最终结果应该是这样的:

         Plant       Pos Strikes Std Dev
0        A           17
1        B           11

我试过了

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

但我每年都会得到如下所示的 NaN:

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

感谢您对此的任何帮助!

【问题讨论】:

    标签: python pandas group-by calculation


    【解决方案1】:

    我相信你只想在Plant 上分组:

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

    输出:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-22
      • 1970-01-01
      • 2022-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-06
      • 1970-01-01
      相关资源
      最近更新 更多