我有一个df,我想根据使用Python所示的thethe列表对其重新排序:
df=pd.DataFrame({'Country':["AU","DE","UR","US","GB","SG","KR","JP","CN"],'Stage #': [3,2,6,6,3,2,5,1,1],'Amount':[4530,7668,5975,3568,2349,6776,3046,1111,4852]})
df
list=["US","CN","GB","AU","JP","KR","UR","DE","SG"]
我该怎么做?有什么想法吗?谢谢!
答案 0 :(得分:2)
list_ = ["US","CN","GB","AU","JP","KR","UR","DE","SG"]
df['Country'] = pd.Categorical(df.Country, categories = list_, ordered = True)
df.sort_values(by='Country')
此外,请勿命名您的变量list
,因为那样会覆盖内置的list
命令