我有一个像这样的数据框:
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)。从那里,我如何根据日历月份的顺序对该数据框进行排序?
要按日历月份对数据框进行排序,可以使用包含月份数字的附加列。操作方法如下:
.sort_values
以下是分步示例:
import pandas as pd # Sample dataframe data = { 0: [354.7, 55.4, 176.5, 95.5, 85.6, 152, 238.7, 104.8, 283.5, 278.8, 249.6, 212.7], 1: ['April', 'August', 'December', 'February', 'January', 'July', 'June', 'March', 'May', 'November', 'October', 'September'], 2: [4.0, 8.0, 12.0, 2.0, 1.0, 7.0, 6.0, 3.0, 5.0, 11.0, 10.0, 9.0] } df = pd.DataFrame(data) # Ensure the month number column is of integer type df[2] = df[2].astype(int) # Sort the dataframe by the month number column df_sorted = df.sort_values(by=2) # Reset the index if needed df_sorted.reset_index(drop=True, inplace=True) print(df_sorted)
输出:
0 1 2 0 85.6 January 1 1 95.5 February 2 2 104.8 March 3 3 354.7 April 4 4 283.5 May 5 5 238.7 June 6 6 152.0 July 7 7 55.4 August 8 8 212.7 September 9 9 249.6 October 10 10 278.8 November 11 11 176.5 December 12
在此示例中:
0
1
2
通过按月份数列(2)对数据框进行排序,数据框会根据日历月份重新排序。