0

For example I have this:

df_1 = pd.DataFrame({'A': [3 for _ in xrange(10)],
                     'B': [3 for _ in xrange(10)],
                     'C': [3 for _ in xrange(10)]})
df_2 = pd.DataFrame({'A': [2 for _ in xrange(5)],
                     'B': [2 for _ in xrange(5)],
                     'C': [2 for _ in xrange(5)]})

print(df_1 - df_2)

and output will be as expected:

    A   B   C
0   1   1   1
1   1   1   1
2   1   1   1
3   1   1   1
4   1   1   1
5 NaN NaN NaN
6 NaN NaN NaN
7 NaN NaN NaN
8 NaN NaN NaN
9 NaN NaN NaN

How can I fill missing values in newly created DF with values from df_1?

1 Answer 1

4

You can use combine_first to fill in the missing values:

In [2]: (df_1 - df_2).combine_first(df_1)
Out[2]:
   A  B  C
0  1  1  1
1  1  1  1
2  1  1  1
3  1  1  1
4  1  1  1
5  3  3  3
6  3  3  3
7  3  3  3
8  3  3  3
9  3  3  3
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.