I have the following dataframe and would like to fill in missing values.
mukey hzdept_r hzdepb_r sandtotal_r silttotal_r
425897 0 61
Using what I just learned a couple questions below....
FYI, this solution will still leave NaN's for any 'Mukey's that don't have any 'sandtotal_r's or 'silttotal_r's.
import pandas as pd
df = pd.read_clipboard()
df1 = df.set_index('mukey')
df1.fillna(df.groupby('mukey').mean(),inplace=True)
df1.reset_index()
mukey hzdept_r hzdepb_r sandtotal_r silttotal_r
0 425897 0 61 5.3 44.70
1 425897 61 152 5.3 44.70
2 425911 0 30 30.1 54.90
3 425911 30 74 17.7 49.80
4 425911 74 84 23.9 52.35