Search code examples
pythonpandasmask

Copy nan values from one dataframe to another


I have two dataframes df1 and df2 of the same size and dimensions. Is there a simple way to copy all the NaN values in 'df1' to 'df2' ? The example below demonstrates the output I want from .copynans()

In: df1
Out:
                   10053802  10053856  10053898  10058054
2012-07-01 00:00:00     100.0     0.353     0.300     0.326
2012-07-01 00:30:00     101.0     0.522     0.258     0.304
2012-07-01 01:00:00     102.0     0.311     0.369     0.228
2012-07-01 01:30:00     103.0       NaN     0.478     0.247
2012-07-01 02:00:00     101.0       NaN       NaN     0.259
2012-07-01 02:30:00     102.0     0.281       NaN     0.239
2012-07-01 03:00:00     125.0     0.320       NaN     0.217
2012-07-01 03:30:00     136.0     0.288       NaN     0.283

In: df2
Out:  
                    10053802  10053856  10053898  10058054
2012-07-01 00:00:00       1.0        2.0      3.0       4.0
2012-07-01 00:30:00       1.0        2.0      3.0       4.0
2012-07-01 01:00:00       1.0        2.0      3.0       4.0
2012-07-01 01:30:00       1.0        2.0      3.0       4.0
2012-07-01 02:00:00       1.0        2.0      3.0       4.0
2012-07-01 02:30:00       1.0        2.0      3.0       4.0
2012-07-01 03:00:00       1.0        2.0      3.0       4.0
2012-07-01 03:30:00       1.0        2.0      3.0       4.0

In: df2.copynans(df1)
Out:
                     10053802  10053856  10053898  10058054
2012-07-01 00:00:00       1.0       2.0       3.0       4.0
2012-07-01 00:30:00       1.0       2.0       3.0       4.0
2012-07-01 01:00:00       1.0       2.0       3.0       4.0
2012-07-01 01:30:00       1.0       NaN       3.0       4.0
2012-07-01 02:00:00       1.0       NaN       NaN       4.0
2012-07-01 02:30:00       1.0       2.0       NaN       4.0
2012-07-01 03:00:00       1.0       2.0       NaN       4.0
2012-07-01 03:30:00       1.0       2.0       NaN       4.0

Solution

  • Either

    df1.where(df2.notnull())
    

    Or

    df1.mask(df2.isnull())