简体   繁体   English

从 csv 文件中对列日期/时间数据进行排序

[英]sort the column date/time data from csv file

hope to get your expert help, I did check on forums first.希望得到您的专家帮助,我确实先检查了论坛。 i am a beginner using repl.it (Python 3.8.2), and the first time use pandas.我是使用 repl.it (Python 3.8.2) 的初学者,第一次使用 pandas。 thanks in advance.提前致谢。

Start_Date_Time column could not be sorted correctly by Python from old to new (even though it can be manually correctly sorted in csv file). Python 无法将 Start_Date_Time 列从旧到新正确排序(即使它可以在 csv 文件中手动正确排序)。 The Python output shows it is sorted by the first number of the cell (Australian format: dd/mm/yyyy) Python output 显示它是按单元格的第一个数字排序的(澳大利亚格式:dd/mm/yyyy)

import pandas as pd
csv1 = pd.read_csv("scheduleList.csv")
sort_by_date = csv1.sort_values("Start_Date_Time")
print (sort_by_date)

Below is simplified csv data.下面是简化的 csv 数据。 I don't want to split the column of date and time, because want to later compare its value with the today's date and time (calculate the difference)我不想拆分日期和时间的列,因为想稍后将其值与今天的日期和时间进行比较(计算差异)

Event   Start_Date_Time       Duration (mins)
A         1/06/2020 7:00            60
B         1/06/2020 12:30           60
C         2/06/2020 18:30           120
D         2/06/2020 16:00           45
E         5/06/2020 17:30           60
F        12/06/2020 12:30           60
G         7/06/2020 13:00           60
H         4/06/2020 18:00           60
I         6/06/2020 11:30           60
J         6/06/2020 8:00            180
K        11/06/2020 12:30           60
L        28/06/2020 11:00           300
M        18/07/2020 19:30           120
N        20/06/2020 9:00            60
O        31/05/2020 10:00           60
P         7/06/2020 10:00           60
Q        14/06/2020 10:00           60

Try the following code.试试下面的代码。 I have added a line to parse Start_Date_Time as a DateTime column and also set ascending parameter of the sort_values method to False .我添加了一行将Start_Date_Time解析为DateTime列,并将sort_values方法的ascending参数设置为False

import pandas as pd
csv1 = pd.read_csv("scheduleList.csv")
csv1['Start_Date_Time'] = pd.to_datetime(csv1['Start_Date_Time']) # change over here
sort_by_date = csv1.sort_values("Start_Date_Time",ascending=False) # change over here
print (sort_by_date)

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

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