我必须根据字典对Dataframe列的值/行进行排序,字典包含Dataframe的每个值及其行索引,但排序不同
howToSortDict = {'Brazil': 2, 'Russia': 0, 'China': 1} # row value and its index
myDict = {
"countries": ["Brazil", "Russia", "China"],
"amount": [1000, 2000, 3000]
}
df = pd.DataFrame(myDict)
它们是如何分类的:
countries amount
0 Brazil 1000
1 Russia 2000
2 China 3000
我需要如何对它们进行排序(作为数据帧):
countries amount
0 Russia 2000
1 China 3000
2 Brazil 1000
如何在不循环每一行的情况下实现这一点
分两步进行:
首先使用
map
创建一个新列,其中包含与国家/地区关联的值然后
sort
和drop
新列使用
argsort
相关问题 更多 >
编程相关推荐