如何根据列表重新排序数据框? pandas

How to reorder a dataframe based on a list? pandas

我有一个 df,我想根据使用 Python:

显示的列表对其重新排序
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"]

我该怎么做?有什么想法吗?谢谢!

使用pd.Categorical

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 命令