Assume I have the following two DataFrames
:
X Y Z
1 0.0 0.0 0.0
2 1.0 2.0 3.0
3 4.0 2.0 0.0
4 NaN NaN NaN
5 NaN NaN NaN
6 NaN NaN NaN
7 NaN NaN NaN
8 NaN NaN NaN
and
X.2 Y.2 Z.2
1 NaN NaN NaN
2 NaN NaN NaN
3 NaN NaN NaN
4 NaN NaN NaN
5 NaN NaN NaN
6 9.0 3.0 6.0
7 7.0 4.0 3.0
8 3.0 6.0 8.0
I would like to fill the missing data in the first DataFrame
with the values from the second. Result should look like this:
X Y Z
1 0.0 0.0 0.0
2 1.0 2.0 3.0
3 4.0 2.0 0.0
4 NaN NaN NaN
5 NaN NaN NaN
6 9.0 3.0 6.0
7 7.0 4.0 3.0
8 3.0 6.0 8.0
If possible I'd like to avoid creating a new DataFrame
but fill up the first DataFrame
in place.
How do I do this?
pandas mean() Key PointsBy default ignore NaN values and performs mean on index axis.
In pandas, a missing value (NA: not available) is mainly represented by nan (not a number). None is also considered a missing value.
You can proceed simply with update
which fills up the first dataframe df1
based on the value of df2
:
df2.columns = df1.columns
df1.update(df2)
In [118]: df1
Out[118]:
X Y Z
1 0 0 0
2 1 2 3
3 4 2 0
4 NaN NaN NaN
5 NaN NaN NaN
6 9 3 6
7 7 4 3
8 3 6 8
If you line the columns up, then fillna() will do this:
df2.columns = df1.column
df1.fillna(df2, inplace=True)
df1
X Y Z
1 0 0 0
2 1 2 3
3 4 2 0
4 NaN NaN NaN
5 NaN NaN NaN
6 9 3 6
7 7 4 3
8 3 6 8
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With