简体   繁体   中英

Sort dataframe by columns names if the columns are dates, pandas?

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

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)]

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]

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM