I have an empty dataframe df1
import numpy as np
import pandas as pd
df1 = pd.DataFrame(columns=['A','B','C','D','E'])
df1
A B C D E
I want to merge or update this dataframe with another dataframe df2
df2 = pd.DataFrame({ 'B': [1,2,3],
'D': [4,5,6],
'E': [7,8,9]})
df2
B D E
0 1 4 7
1 2 5 8
2 3 6 9
to get a merged or updated dataframe as
A B C D E
0 NaN 1 NaN 4 7
1 NaN 2 NaN 5 8
2 NaN 3 NaN 6 9
Besides, efficiency is required because I have a long df1
and df2
.
Any good idea? Thank you.
CodePudding user response:
try this
import pandas as pd
pd.concat((df1, df2))
CodePudding user response:
Use reindex
:
df1 = df2.reindex(columns=df1.columns)
Output:
A B C D E
0 NaN 1 NaN 4 7
1 NaN 2 NaN 5 8
2 NaN 3 NaN 6 9