python - 按索引和列排序

标签 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'

最佳答案

在上一个 pandas 版本 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

另一个适用于某些较旧的 pandas 版本的解决方案是将 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

关于python - 按索引和列排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58318652/

相关文章:

python - 计算两个数据帧的振荡

python - 按字母顺序对单元格文本排序

javascript - 使用纯 JavaScript 排序表客户端,无需任何库

python - 使用结构化数组来命名 numpy 数组中的轴

Python,获取列表列表中最大值的索引

python - 没有 celery 的Django后台处理

unix - 我们如何使用 unix sort 更快地排序?

Python IDLE子进程错误?

python - 如何按不在数据框中的数组对数据框进行排序

c++ - 如何使用 CUDA/Thrust 根据其中一个数组中的值对两个数组/vector 进行排序