从字符串列表中提取某些元素,并使用Pandas转换为日期时间 [英] Extracting certain elements from a list of a string and turning into datetime with Pandas

查看:288
本文介绍了从字符串列表中提取某些元素,并使用Pandas转换为日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表,例如,称为X. X在特定目录中具有多个文件名.例如:

I have a a list, which for example is called X. X has number of file names within a particular directory. For example:

X = ['director_send_20140212', 'send_help20150315', 'hello_jeep_20160322'....etc]

现在,我想从这些列表中提取日期并将其转换为日期的datetime索引,以便可以使用它按日期对数据框进行索引.因此,从上面的示例中,我只想要三个日期...但是实际上,这是一个更大的文件名集合

Now, I want to extract the dates out of this these lists and convert it into a datetime index of dates so I can use it to index dataframe by dates. So from above example I only want the three dates...but in reality it's a larger collection of file names

为了使生活更轻松,日期都以20开头(即2000年及以后),并且在任何情况下都不会在文件名的其他任何位置出现20.此外,格式为yyyymmdd/.

To make life easier the dates all begin with 20 (i.e. year 2000 and beyond), and there are no situations where 20 appears anywhere else in the file name. Additionally, the format is yyyymmdd/.

所以我想使用熊猫提供的datetimes索引来创建日期范围!

So I want to create a range of dates using the datetimes index provided by pandas!

推荐答案

我正在对文件名的命名约定做一些假设.主要是日期是下划线之后的数据的最后部分,并且最后部分不包含不是日期的数字数据.

I'm making a few assumptions about the naming conventions of your file names. Mainly that the dates are the last portion of data after the underscore, and that the last portion doesn't contain numeric data that isn't the date.

话虽如此,这里是一个示例列表理解:

That being said, here is an example list comprehension:

>>> from datetime import datetime
>>> [datetime.strptime(''.join(c for c in file_name.split('_')[-1] if c.isdigit()), "%Y%m%d") for file_name in X]
[datetime.datetime(2014, 2, 12, 0, 0), datetime.datetime(2015, 3, 15, 0, 0), datetime.datetime(2016, 3, 22, 0, 0)]

这篇关于从字符串列表中提取某些元素,并使用Pandas转换为日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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