Replace column values within a groupby and condition

前端 未结 1 371
谎友^
谎友^ 2021-01-28 00:54

I have a dataframe that I want to find the minimum value of a column within a group, and then based on that row, update the values of some of the other columns.

The foll

相关标签:
1条回答
  • 2021-01-28 01:07

    Use groupby on ID + transform + idxmin on Year to get a series of indices. Pass these indices to loc to get your result.

    (df.iloc[df.groupby('ID')['Year'].transform('idxmin')]
       .reset_index(drop=True)
       .assign(Albedo=df['Albedo']))
    
       Albedo  ID  Precip  Temp  Year
    0     0.2   1     200    20  1950
    1     0.4   1     200    20  1950
    2     0.5   1     200    20  1950
    3     0.3   2      45     5  1916
    4     0.5   2      45     5  1916
    5     0.1   2      45     5  1916
    
    0 讨论(0)
提交回复
热议问题