【发布时间】:2017-03-26 04:00:19
【问题描述】:
我在 pandas 数据框中快速读取 203 mb 文件时遇到了一些问题。我想知道是否有更快的方法可以做到这一点。以下是我的功能:
import pandas as pd
import numpy as np
def file(filename):
df = pd.read_csv(filename, header=None, sep='delimiter', engine='python', skiprows=1)
df = pd.DataFrame(df[0].str.split(',').tolist())
df = df.drop(df.columns[range(4,70)], axis=1)
df.columns = ['time','id1','id2','amount']
return df
当我使用魔术%timeit 函数时,读取文件并将其上传到 python 笔记本大约需要 6 秒。我该怎么做才能加快速度?
谢谢!
【问题讨论】:
-
engine='python'- 来自文档:C 引擎更快,而 python 引擎目前功能更完整 - 如果可以的话,坚持使用 C 引擎。跨度> -
@tdelaney,没错,但是...来自文档:
Separators longer than 1 character and different from ‘s+’ will be interpreted as regular expressions, will force use of the python parsing engine and will ignore quotes in the data. -
@kma,您能否发布 CSV 的 2-3 行样本(即类似格式)?
-
@MaxU -
sep='delimiter'意思是......好吧,我不太确定。这可能意味着示例不正确。 -
@tdelaney,这是一个很好的观点!
标签: python csv pandas dataframe data-science