我对以日期时间索引的熊猫数据框有问题,我的数据框temps
如下所示:
column column
Index | land_temps | ocean_temps
1861-01-01 | -5 | 15
1861-02-01 | 0 | 17
1861-03-01 | 6 | 18
.
.
.
2015-11-01 | 2 | 17
2015-12-01 | -1 | 14
总而言之,我有一个熊猫数据框,日期为datatimeindexes,浮点数(温度)为列。 我想按monts of measurement对这个数据框的rekord进行排序,以达到如下目的:
Index | land_temps | ocean_temps
1861-01-01 | -5 | 15
1862-02-01 | -4 | 13
1863-03-01 | -6 | 14
.
.
.
2014-12-01 | -2 | 13
2015-12-01 | -1 | 14
该怎么做?我尝试过:
temps.sort_values(by=temps.index.month, axis='index')
但是它不能像我想的那样工作,所以有什么方法可以使用在sorting / groupby panadas方法(或类似方法)中进行构建。
预先感谢:)。
答案 0 :(得分:2)
您可以创建另外两个列(以后可以将其删除),然后可以根据这些列进行排序
temps['month'] = temps.index.month
temps['year'] = temps.index.year
temps.sort_values(['month', 'year']).drop(columns=['month', 'year'])