如何使用 pandas 解析CSV文件? [英] How to parse CSV file using pandas?

查看:87
本文介绍了如何使用 pandas 解析CSV文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我有了一个.csv文件,其中有一列时间,例如"20140203 00:00:03.132",我该如何有效地将秒数部分(:03.132")放下?数据量巨大,我尝试使用sed预处理数据,但是速度太慢了!

Now I have a .csv file, with a column of time, such that "20140203 00:00:03.132", how can I drop the seconds part(":03.132") efficiently? The data amount is huge, and I tried preprocess the data using sed but it was too slow!

我现在正在尝试解析熊猫中的.csv文件.无论如何,我可以有效地处理吗?也欢迎使用除熊猫以外的方法!

I am now trying parse the .csv file in pandas. Is there anyway I could handle that efficiently? Methods other than pandas is also welcomed!

推荐答案

有一个方便的时间戳解析库:日期时间:

There is a handy library for parsing timestamps: datetime:

import datetime
x = '20140203 00:00:03.132'
timestamp = datetime.datetime.strptime(x, '%Y%m%d %H:%M:%S.%f')
print datetime.datetime.strftime(timestamp, '%Y%m%d %H:%M')  # 20140203 00:00

或者由于处理大量数据的速度有点慢,您可以从第一个:的右边开始拆分,然后获取结果列表的第一个元素:

Or since it's a bit slow for a huge amount of data, you can split from the right on the first : and then take the first element of the resulting list:

print x.rsplit(':', 1)[0]  # 20140203 00:00

这篇关于如何使用 pandas 解析CSV文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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