按另一个索引的顺序对 Pandas Dataframe 进行排序 [英] Sorting Pandas Dataframe by order of another index

查看:73
本文介绍了按另一个索引的顺序对 Pandas Dataframe 进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个数据帧,df1 和 df2,它们共享相同的索引.df1 按照我希望 df2 排序的顺序进行排序.

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.

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?

谢谢!

推荐答案

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

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

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

这篇关于按另一个索引的顺序对 Pandas Dataframe 进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆