Say I have two data frames: df1 and df2, which have the same index. df1 is sorted in the order in which I want to sort df2.
df=pd.DataFrame(index=['Arizona','New Mexico', 'Colorado'],columns=['A','B','C'], data=[[1,2,3],[4,5,6],[7,8,9]]) print df ABC Arizona 1 2 3 New Mexico 4 5 6 Colorado 7 8 9 df2=pd.DataFrame(index=['Arizona','Colorado', 'New Mexico'], columns=['D'], data=['Orange','Blue','Green']) print df2 D Arizona Orange Colorado Blue New Mexico Green
What is the best / most efficient way to sort the second block of data by index of the first?
One option is to simply combine them, sort, and then delete the columns:
df.join(df2)[['D']] D Arizona Orange New Mexico Green Colorado Blue
Is there a more elegant way to do this?
Thanks!
source share