按另一个索引的顺序对 Pandas Dataframe 进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31843911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:44:36  来源:igfitidea点击:

Sorting Pandas Dataframe by order of another index

pythonpandas

提问by AJG519

Say I have two dataframes, df1 and df2 that share the same index. df1 is sorted in the order that I want df2 to be sorted.

假设我有两个数据帧,df1 和 df2,它们共享相同的索引。df1 按照我希望 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

            A  B  C
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 of sorting the second dataframe by the index of the first?

按第一个数据帧的索引对第二个数据帧进行排序的最佳/最有效方法是什么?

One option is just joining them, sorting, and then dropping the columns:

一种选择是加入它们,排序,然后删除列:

df.join(df2)[['D']]

                 D
Arizona     Orange
New Mexico   Green
Colorado      Blue

Is there a more elegant way of doing this?

有没有更优雅的方法来做到这一点?

Thanks!

谢谢!

回答by chrisb

reindexwould work - be aware that it will create missing values for index values that are df, not in df2.

reindex会起作用 - 请注意,它会为 df 而不是 df2 的索引值创建缺失值。

In [18]: df2.reindex(df.index)
Out[18]: 
                 D
Arizona     Orange
New Mexico   Green
Colorado      Blue