How to recover the initial structure after update one Python pandas dataframe with another dataframe?

Viewed 62

Here is the original post: Python Pandas update a dataframe value from another dataframe

df1 and df2 have the same data structure.

The original column order: assignee id issuetype key

The problem is that after runing the following code:

df1.set_index('key',inplace=True)
df1.update(df2.set_index('key'))
df1.reset_index() 

The column order of df1 changed to this:

key assignee id issuetype

How can I recover the initial structure after update one dataframe with another dataframe? thanks.

1 Answers

Change order by original columns by DataFrame.reindex:

cols = df1.columns
df1.set_index('key',inplace=True)
df1.update(df2.set_index('key'))
df1 = df1.reset_index().reindex(cols, axis=1)
print (df1)
Related