I am trying to sort by an index and column but to no avail.
Partial dataset
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
index column
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)
My attempt
df2 = df2.rename_axis(df2.index).sort_values(by = [df2.index, 'ID'], ascending = [False, True])
Output from above: ValueError: Length of new names must be 1, got 165002
df2 = df2.rename_axis("Date").sort_values(by = ["Date", "ID"], ascending = [False, True])
Output from above: KeyError: 'Date'
df2 = df2.sort_values(by = [df2.index, 'ID'], ascending = [False, True])
Output from above: 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) not in index"
df2 = df2.sort_values(by = ["Date", "ID"], ascending = [False, True])
Output from above: KeyError: 'Date'
df2 = df2.sort_values(by = [df2.index.Date, 'ID'], ascending = [False, True])
Output from above: AttributeError: 'DatetimeIndex' object has no attribute 'Date'