简体   繁体   English

如果列是日期,大熊猫,则按列名称对数据框进行排序?

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

my df columns names are dates in this format: dd-mm-yy. 我的df列名称是以下格式的日期: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. 当我使用sort_index(axis = 1)时,它按前两位数字(指定日期)排序,因此按时间顺序没有意义。 How can I sort it automatically by taking into account also the months? 如何考虑到月份又如何自动对它进行排序?

my df headers: 我的df标头:

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. 将列转换为日期时间,并使用argsort查找正确的顺序。 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 只需将您的列转换为DateTime

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

and then sort it using sort_values 然后使用sort_values对其进行排序

  df.sort_values(by='newdate')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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