Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 按索引和列排序_Python_Pandas_Sorting_Datetime_Datetimeindex - Fatal编程技术网

Python 按索引和列排序

Python 按索引和列排序,python,pandas,sorting,datetime,datetimeindex,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

我试图按索引和列进行排序,但没有用

部分数据集

            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)
索引列

            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,got 165002

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

上面的输出:KeyError:“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、2005-01-01、2015-12-31、2015-12-31、2015-12-31、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)不在索引中“

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

上面的输出:AttributeError:“DatetimeIndex”对象在上一版本中没有属性“Date”

此工作模式:

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

您的pandas版本是什么?因为在上一个版本中它工作得很好。@jezrael“0.19.2”是一个课程作业,在他们指定的服务器环境中完成。为它添加了解决方案。或者,
df.set\u index('ID',append=True)。sort\u index(升序=[False,True])。reset\u index('ID')
df2 = (df2.reset_index()
          .sort_values(by = ["Date", "ID"], ascending = [False, True])
          .set_index('Date'))
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