1

my df columns names are dates in this format: dd-mm-yy. when I use sort_index(axis = 1) it sort by the first two digits (which specify the days) so it doesn't make sense chronologically. How can I sort it automatically by taking into account also the months?

my df headers:

submitted_at             06-05-18  13-05-18  29-04-18

I expected the output of:

submitted_at             29-04-18  06-05-18  13-05-18

3 Answers 3

2

Convert the columns to datetime and use argsort to find the correct ordering. This will put all non-dates to the left in the order they occur, followed by the sorted dates.

import pandas as pd
df = pd.DataFrame(columns=['submitted_at', '06-05-18', '13-05-18', '29-04-18'])

idx = pd.to_datetime(df.columns, errors='coerce', format='%d-%m-%y').argsort()
df.iloc[:, idx]

Empty DataFrame
Columns: [submitted_at, 29-04-18, 06-05-18, 13-05-18]
Sign up to request clarification or add additional context in comments.

Comments

1

Converting strings to datetime then sorting them with something like this :

from datetime import datetime
cols_as_date = [datetime.strptime(x,'%d-%m-%Y') for x in df.columns]
df = df[sorted(cols_as_data)]

Comments

0

just convert to DateTime your column

df['newdate']=pd.to_datetime(df.date,format='%d-%m-%y')

and then sort it using sort_values

  df.sort_values(by='newdate')

Comments

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.