如何对包含12小时(AM/PM)格式的值的 pandas 时间序列进行排序 [英] How to sort a Pandas Time Series containing values in 12 hour (AM/PM) format

查看:78
本文介绍了如何对包含12小时(AM/PM)格式的值的 pandas 时间序列进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用熊猫来处理csv文件中的某些数据.

I am using pandas to process some data from csv files.

我需要按列MEETING START TIME对DataFrame df中的数据进行排序,只是对时间进行排序.日期由另一个字段处理.

I need to sort the data in my DataFrame df by the column MEETING START TIME, just sorting the time. The date is handled by another field.

但是我得到的结果是:

MEETING START TIME
10:30 AM
12:30 PM
2:00 PM
4:00 PM
9:15 AM
9:15 AM

早上任何以一位数小时表示的会议时间都将结束.我需要对日期格式或排序命令进行某些操作吗?

Any meeting time in the morning with single-digit hours goes as the end. Do I need to do something to the date format, or the sort command?

推荐答案

您可以使用pd.datetools.parse尝试将日期列(当前为字符串)转换为日期时间对象;那么您应该可以进行排序:

You can use pd.datetools.parse to try and convert your date column (currently a string) into a datetime object; then you should be able to sort:

df = pd.DataFrame({'MEETING START TIME': ['10:30 AM','12:30 PM', '2:00 PM', '4:00 PM', '9:15 AM', '9:15 AM']})
df['MEETING START TIME'] = df['MEETING START TIME'].map(lambda x: pd.datetools.parse(x))
df.sort('MEETING START TIME')

Out[33]: 
   MEETING START TIME
5 2015-08-05 09:15:00
4 2015-08-05 09:15:00
0 2015-08-05 10:30:00
1 2015-08-05 12:30:00
2 2015-08-05 14:00:00
3 2015-08-05 16:00:00

这篇关于如何对包含12小时(AM/PM)格式的值的 pandas 时间序列进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆