1

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
  • The problem is that:
    1. I don't know how to arrange columns orders when it's represented as datetime info.
    2. The table above seems strange since that "Unnamed: 0" row is empty and there's a space between the columns and rows unlike other ordinary dataframes.
1
  • What is print (df.info()) ? Commented Nov 6, 2020 at 8:35

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks you so much for the solution! It finally solved my problem thank you again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.