Reputation: 43
I often need to reindex dataframe on both the index and columns of another dataframe, i.e.
df_out = df2.reindex(df1.index).reindex(df1.columns,axis='columns')
Is there a more efficient way to do this? As in quicker execution. Or less verbose.
Upvotes: 2
Views: 954
Reputation: 88246
reindex
setting index and columns in one callreindex
can take both index
and columns
parameters in one call to do exactly that:
df2.reindex(index=df1.index, columns=df1.columns)
reindex_like
to reindex just like another dataframe (both axes):df2.reindex_like(df1)
Let's check with the same example from the docs:
print(df)
http_status response_time
Firefox 200 0.04
Chrome 200 0.02
Safari 404 0.07
IE10 404 0.08
Konqueror 301 1.00
new_index = ['Safari', 'Iceweasel', 'Comodo Dragon', 'IE10','Chrome']
new_columns = ['http_status', 'some_other_col']
df.reindex(index=new_index, columns=new_columns)
http_status some_other_col
Safari 404.0 NaN
Iceweasel NaN NaN
Comodo Dragon NaN NaN
IE10 404.0 NaN
Chrome 200.0 NaN
Upvotes: 2