Reputation: 393
I have a dataframe table that has columns containing datetime information. As you can see from the table below, the 2019-xx field is between the years 2018 and 2016 so I need to arrange it properly.
I tried to use .sort_index(axis=1, inplace=True)
but in vain
(I don't know why it has no effect at all).
Dataframe:
2017-12-31 2018-12-31 2019-12-31 2016-12-31 2020-06-30
Unnamed: 0
WaterFlow -26084000.0 -257404000.0 -84066000.0 135075000.0 NaN
trailing1HourWaterFlow NaN NaN -84066000.0 NaN 6823000.0
"Unnamed: 0"
row is empty
and there's a space between the columns and rows unlike other
ordinary dataframes.Upvotes: 1
Views: 79
Reputation: 862511
I think you need convert the columns to datetimes, then do the sorting. If Unnamed: 0
is the index name you can remove it by using DataFrame.rename_axis
:
df.columns = pd.to_datetime(df.columns)
df = df.sort_index(axis=1).rename_axis(None)
Upvotes: 2