【问题标题】:Pandas create column with the maximum value based on duplicate values from another column熊猫根据另一列的重复值创建具有最大值的列
【发布时间】:2021-12-05 04:20:20
【问题描述】:

大家好,我有这个数据框。我正在尝试创建 2 个附加列,max_temperature 和 min_temperature 来记录基于stayid 的最高和最低温度值。我该怎么做?

【问题讨论】:

    标签: python-3.x pandas


    【解决方案1】:

    尝试 groupby、agg 和 pd.join

      newdf=(df.set_index('stayid')#Set stayid to allow joining of the aggregated to the main df
        .join(# This joins the ggregated df to main df
            df.groupby('stayid')['temp'].agg([min,max])# Compute the min and max temperature and put them into a summarised df
        ).rename(columns={'min':'min_temp', 'max':'max_temp'}))#rename the min and max columns)
    

    【讨论】:

      猜你喜欢
      • 2020-07-07
      • 1970-01-01
      • 2018-08-15
      • 1970-01-01
      • 2022-12-16
      • 2023-02-05
      • 2020-08-09
      • 2022-01-01
      • 2020-12-11
      相关资源
      最近更新 更多