python - 使用 Python 读取 csv 文件第 i 列的最佳方法是什么?

标签 python r python-2.7 csv import-from-csv

我习惯了 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/

相关文章:

r - 派对套件 : Displaying terminal node percentile values above terminal node boxplots

r - Shapiro 测试 : is. 数字 (x) 不为 TRUE

python - 为 C 库生成 Python SWIG 绑定(bind)时 undefined symbol

python - 将值除以三个列表并比较其中的元素

R点阵条形图: How to write the total sum on each bar in multiple panels?

Tornado Web 应用程序中的 Python xmpp jabber 客户端

qt - QGraphicsView 双击事件和 ScrollHandDrag 模式项问题

python - 我的绘图和 FWHM 计算出现错误

python - 检查字符串是否包含列表中字符串的最快方法

python - pywinauto:从 MS Edge Canary 地址栏中获取 URL