fillna with max value of each group in python

Dataframe

df=pd.DataFrame({"sym":["a","a","aa","aa","aa","a","ab","ab","ab"],
                "id_h":[2.1, 2.2 , 2.5 , 3.1 , 2.5, 3.8 , 2.5, 5,6],
                 "pm_h":[np.nan, 2.3, np.nan , 2.8, 2.7, 3.7, 2.4, 4.9,np.nan]})

want to fill pm_h nan values with max id_h value of each “sys” group i.e. (a, aa, ab)

Required output:

df1=pd.DataFrame({"sym":["a","a","aa","aa","aa","a","ab","ab","ab"],
                "id_h":[2.1, 2.2 , 2.5 , 3.1 , 2.5, 3.8 , 2.5, 5,6],
                 "pm_h":[3.8, 2.3, 3.1 , 2.8, 2.7, 3.7, 2.4, 4.9, 6})

Answer

Use Series.fillna with GroupBy.transform by maximal values for new Series with same index like original:

df['pm_h'] = df['pm_h'].fillna(df.groupby('sym')['id_h'].transform('max'))
print (df)
  sym  id_h  pm_h
0   a   2.1   3.8
1   a   2.2   2.3
2  aa   2.5   3.1
3  aa   3.1   2.8
4  aa   2.5   2.7
5   a   3.8   3.7
6  ab   2.5   2.4
7  ab   5.0   4.9
8  ab   6.0   6.0