我习惯了 R,它提供了逐列读取 CSV 文件的快速功能,任何人都可以提出一种快速并且在 python 中读取大数据(例如 CSV)文件的有效方法吗?例如,CSV 文件的第 ith 列。
我有以下但需要时间:
import os,csv, numpy, scipy
from numpy import *
f= open('some.csv', 'rb')
reader = csv.reader(f, delimiter=',')
header = reader.next()
zipped = zip(*reader)
print( zipped[0] ) # is the first column
有没有更好的方法在 python 中读取数据(从大文件)(至少在内存方面与 R 一样快)?
最佳答案
您还可以使用 pandas.read_csv
及其 use_cols
参数。参见 here
import pandas as pd
data = pd.read_csv('some.csv', use_cols = ['col_1', 'col_2', 'col_4'])
...
关于python - 使用 Python 读取 csv 文件第 i 列的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16863485/