按索引和列排序

时间:2019-10-10 08:33:21

标签: python pandas sorting datetime datetimeindex

我正在尝试按索引和列进行排序,但无济于事。

部分数据集

            ID         Element  Data_Value
Date            
2005-01-01  USW00004848 TMIN    0
2005-01-01  USC00207320 TMAX    150
2005-01-01  USC00207320 TMIN    -11
2005-01-01  USW00014833 TMIN    -44
2005-01-01  USW00014833 TMAX    33

索引列

DatetimeIndex(['2005-01-01', '2005-01-01', '2005-01-01', '2005-01-01',
               '2005-01-01', '2005-01-01', '2005-01-01', '2005-01-01',
               '2005-01-01', '2005-01-01',
               ...
               '2015-12-31', '2015-12-31', '2015-12-31', '2015-12-31',
               '2015-12-31', '2015-12-31', '2015-12-31', '2015-12-31',
               '2015-12-31', '2015-12-31'],
              dtype='datetime64[ns]', name='Date', length=165002, freq=None)

我的尝试

df2 = df2.rename_axis(df2.index).sort_values(by = [df2.index, 'ID'], ascending = [False, True])

上面的输出: ValueError:新名称的长度必须为1,为165002

df2 = df2.rename_axis("Date").sort_values(by = ["Date", "ID"], ascending = [False, True])

上面的输出: KeyError:“日期”

df2 = df2.sort_values(by = [df2.index, 'ID'], ascending = [False, True]) 

上面的输出: KeyError:“ DatetimeIndex(['2005-01-01','2005-01-01','2005-01 -01','2005-01-01',\ n'2005-01-01','2005-01-01','2005-01-01','2005-01-01',\ n'2005 -01-01','2005-01-01',\ n ... \ n'2015-12-31','2015-12-31','2015-12-31','2015-12- 31',\ n'2015-12-31','2015-12-31','2015-12-31','2015-12-31',\ n'2015-12-31','2015- 12-31'],\ n dtype ='datetime64 [ns]',name ='Date',length = 165002,freq = None)不在索引中”

df2 = df2.sort_values(by = ["Date", "ID"], ascending = [False, True])

上面的输出: KeyError:“日期”

df2 = df2.sort_values(by = [df2.index.Date, 'ID'], ascending = [False, True]) 

上面的输出: AttributeError:'DatetimeIndex'对象没有属性'Date'

1 个答案:

答案 0 :(得分:1)

在最新的熊猫版本0.23+中,此方法很不错:

print (df2.index)
DatetimeIndex(['2005-01-01', '2005-01-01', '2005-01-01', '2005-01-01',
               '2005-01-01'],
              dtype='datetime64[ns]', name='Date', freq=None)


df2 = df2.sort_values(by = ["Date", "ID"], ascending = [False, True])
print (df2)
                     ID Element  Data_Value
Date                                       
2005-01-01  USC00207320    TMAX         150
2005-01-01  USC00207320    TMIN         -11
2005-01-01  USW00004848    TMIN           0
2005-01-01  USW00014833    TMIN         -44
2005-01-01  USW00014833    TMAX          33

在某些较早的熊猫版本中也可以使用的另一种解决方案是将DatetimeIndex转换为列首,进行排序并转换回:

df2 = (df2.reset_index()
          .sort_values(by = ["Date", "ID"], ascending = [False, True])
          .set_index('Date'))

感谢@Alexander供选择:

df2 = (df.set_index('ID', append=True)
         .sort_index(ascending=[False, True])
         .reset_index('ID'))

print (df2)
                     ID Element  Data_Value
Date                                       
2005-01-01  USC00207320    TMAX         150
2005-01-01  USC00207320    TMIN         -11
2005-01-01  USW00004848    TMIN           0
2005-01-01  USW00014833    TMIN         -44
2005-01-01  USW00014833    TMAX          33