0
df1 = pd.DataFrame(columns=['a','B','c','D'])    
df2 = pd.DataFrame({'a':[1,2],'B':[3,4]})

I'd like to update the empty columns in df1 with df2 but keeping the original column order. I tried

df1.combine_first(df2)

but this changes the order of the columns

   B   D  a   c
0  3 NaN  1 NaN
1  4 NaN  2 NaN
1

1 Answer 1

1

Try with reindex

df2.reindex(df1.columns, axis=1)
Out[44]: 
   a  B   c   D
0  1  3 NaN NaN
1  2  4 NaN NaN
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.