Fill in missing row values in pandas dataframe

后端 未结 1 1577
迷失自我
迷失自我 2021-01-24 16:40

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                 


        
相关标签:
1条回答
  • 2021-01-24 17:14

    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
    
    0 讨论(0)
提交回复
热议问题