Pandas fill missing values in dataframe from another dataframe

前端 未结 5 879
遥遥无期
遥遥无期 2020-11-28 12:10

I cannot find a pandas function (which I had seen before) to substitute the NaN\'s in a dataframe with values from another dataframe (assuming a common index which can be sp

5条回答
  •  清酒与你
    2020-11-28 12:26

    A dedicated method for this is DataFrame.update:

    Quoted from the documentation:

    Modify in place using non-NA values from another DataFrame.
    Aligns on indices. There is no return value.

    Important to note is that this method will modify your data inplace. So it will overwrite your updated dataframe.

    Example:

    print(df1)
           A    B     C
    aaa  NaN  1.0   NaN
    bbb  NaN  NaN  10.0
    ccc  3.0  NaN   6.0
    ffffd  NaN  NaN   NaN
    eee  NaN  NaN   NaN
    
    print(df2)
             A    B     C
    index                
    aaa    1.0  1.0   NaN
    bbb    NaN  NaN  10.0
    eee    NaN  1.0   NaN
    
    # update df1 NaN where there are values in df2
    df1.update(df2)
    print(df1)
           A    B     C
    aaa  1.0  1.0   NaN
    bbb  NaN  NaN  10.0
    ccc  3.0  NaN   6.0
    ffffd  NaN  NaN   NaN
    eee  NaN  1.0   NaN
    

    Notice the updated NaN values at intersect aaa, A and eee, B

提交回复
热议问题