Skip to content Skip to sidebar Skip to footer

How To Replace Missing Data In Dataframe

Lets say I have the following DataFrame: df = pd.DataFrame({'col1': [241, 123, 423], 'col2':[977, 78, np.NaN], 'col3':[76, 432, np.NaN], 'col4':[234, 321, 987]}, index=pd.date_rang

Solution 1:

We can use DataFrame.fillna:

df=df.fillna(df2)
print(df)

            col1   col2   col3  col4
Date                                
2019-01-01   241  977.0   76.0   234
2019-01-02   123   78.0  432.0   321
2019-01-03   423  111.0  222.0   987

if you had a series by columns like the one obtained with df2.iloc[0] we can also do it:

my_serie=df2.iloc[0]print(my_serie)col2111col3222Name:2019-01-03 00:00:00,dtype:int64print(df.fillna(my_serie))col1col2col3col4Date2019-01-01   241977.076.02342019-01-02   12378.0432.03212019-01-03   423111.0222.0987

Solution 2:

Alternative combine_first

df2.combine_first(df)Out[8]:col1col2col3col4Date2019-01-01  241.0977.076.0234.02019-01-02  123.078.0432.0321.02019-01-03  423.0111.0222.0987.0

Or update

df.update(df2)dfOut[10]:col1col2col3col4Date2019-01-01   241977.076.02342019-01-02   12378.0432.03212019-01-03   423111.0222.0987

Post a Comment for "How To Replace Missing Data In Dataframe"