熊猫系列中的值升序排列,重新分配时不起作用

问题描述 投票:0回答:1

我正在尝试按升序对Pandas系列进行排序。

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

这些值以升序

但是,当我随后在数据框的上下文中修改系列时:

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

为什么这给了我与上面不同的输出?

请问有什么建议吗?

python pandas dataframe sorting series
1个回答
0
投票
Top15['HighRenew'] = Top15['HighRenew'].sort_values(ascending=True).to_numpy() 

Top15['HighRenew'] = Top15['HighRenew'].sort_values(ascending=True).reset_index(drop=True)

当您对sort_values进行排序时,索引不变,因此它按索引对齐!

谢谢你!”>

© www.soinside.com 2019 - 2024. All rights reserved.