重新分配时,按升序对 pandas 系列中的值进行排序不起作用
问题描述:
我正在尝试按升序对Pandas系列进行排序.
I am trying to sort a Pandas Series in ascending order.
Top15['HighRenew'].sort_values(ascending=True)
给我:
Country
China 1
Russian Federation 1
Canada 1
Germany 1
Italy 1
Spain 1
Brazil 1
South Korea 2.27935
Iran 5.70772
Japan 10.2328
United Kingdom 10.6005
United States 11.571
Australia 11.8108
India 14.9691
France 17.0203
Name: HighRenew, dtype: object
值按升序.
但是,当我随后在数据框的上下文中修改系列时:
However, when I then modify the series in the context of the dataframe:
Top15['HighRenew'] = Top15['HighRenew'].sort_values(ascending=True)
Top15['HighRenew']
给我:
Country
China 1
United States 11.571
Japan 10.2328
United Kingdom 10.6005
Russian Federation 1
Canada 1
Germany 1
India 14.9691
France 17.0203
South Korea 2.27935
Italy 1
Spain 1
Iran 5.70772
Australia 11.8108
Brazil 1
Name: HighRenew, dtype: object
为什么这给了我与上面不同的输出?
Why this is giving me a different output to that above?
请问您有什么建议吗?
答
Top15['HighRenew'] = Top15['HighRenew'].sort_values(ascending=True).to_numpy()
或
Top15['HighRenew'] = Top15['HighRenew'].sort_values(ascending=True).reset_index(drop=True)
当您对sort_values进行排序时,索引不会更改,因此它会根据索引进行对齐!
When you sort_values , the indexes don't change so it is aligning per the index!
感谢anky为我提供了这个出色的解决方案!
Thank you to anky for providing me with this fantastic solution!