简体   繁体   中英

Replace column values within a groupby and condition

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 following code does what I want:

import pandas as pd

df = pd.DataFrame({'ID': [1,1,1,2,2,2,],
                   'Albedo': [0.2, 0.4, 0.5, 0.3, 0.5, 0.1],
                   'Temp' : [20, 30, 15, 40, 10, 5],
                   'Precip': [200, 100, 150, 60, 110, 45],
                   'Year': [1950, 2000, 2004, 1999, 1976, 1916]})

#cols to replace values for
cols = ['Temp', 'Precip', 'Year']

final = pd.DataFrame()


for key, grp in df.groupby(['ID']):

    #minimum values based on year
    replace = grp.loc[grp['Year'] == grp['Year'].min()]

    #replace the values
    for col in cols:
        grp[col] = replace[col].unique()[0]  

    #append the values
    final = final.append(grp)
print(final)

which yields:

   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

so within each group from ID I find the minimum Year and then update Temp , Precip and the Year of the other rows. This seems like a lot of looping and I am wondering if there is a better way though.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM