如何从一列对 pandas 数据框进行排序 [英] how to sort pandas dataframe from one column
问题描述
我有一个这样的数据框:
I have a data frame like this:
print(df)
0 1 2
0 354.7 April 4.0
1 55.4 August 8.0
2 176.5 December 12.0
3 95.5 February 2.0
4 85.6 January 1.0
5 152 July 7.0
6 238.7 June 6.0
7 104.8 March 3.0
8 283.5 May 5.0
9 278.8 November 11.0
10 249.6 October 10.0
11 212.7 September 9.0
如您所见,月份不是按日历顺序排列的.所以我创建了第二列来获取每个月(1-12)对应的月数.从那里,我如何根据日历月的顺序对这个数据框进行排序?
As you can see, months are not in calendar order. So I created a second column to get the month number corresponding to each month (1-12). From there, how can I sort this data frame according to calendar months' order?
推荐答案
使用 sort_values
按特定列的值对 df 进行排序:
Use sort_values
to sort the df by a specific column's values:
In [18]:
df.sort_values('2')
Out[18]:
0 1 2
4 85.6 January 1.0
3 95.5 February 2.0
7 104.8 March 3.0
0 354.7 April 4.0
8 283.5 May 5.0
6 238.7 June 6.0
5 152.0 July 7.0
1 55.4 August 8.0
11 212.7 September 9.0
10 249.6 October 10.0
9 278.8 November 11.0
2 176.5 December 12.0
如果您想按两列排序,请将列标签列表传递给 sort_values
,并根据排序优先级对列标签进行排序.如果您使用 df.sort_values(['2', '0'])
,结果将按列 2
排序,然后按列 0
.当然,这对于这个例子来说没有意义,因为 df['2']
中的每个值都是唯一的.
If you want to sort by two columns, pass a list of column labels to sort_values
with the column labels ordered according to sort priority. If you use df.sort_values(['2', '0'])
, the result would be sorted by column 2
then column 0
. Granted, this does not really make sense for this example because each value in df['2']
is unique.
这篇关于如何从一列对 pandas 数据框进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!