如何在python中将CSV文件导入为numpy.array? [英] How to import csv file as numpy.array in python?

查看:612
本文介绍了如何在python中将CSV文件导入为numpy.array?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个csv file.csv格式:

say i have a csv file.csv in this format:

dfaefew,432,1
vzcxvvz,300,1
ewrwefd,432,0

如何将第二列作为numpy.array导入,以及将第三列作为另一列导入,如下所示:

how to import the second column as a numpy.array and the third column as another one like this:

second = np.array([432, 300, 432])
third = np.array([1, 1, 0])

我正在Ubuntu中使用python2.7.

I am using python2.7 in Ubuntu.

谢谢!

推荐答案

numpy.genfromtxt()是在此处使用的最佳方法

numpy.genfromtxt() is the best thing to use here

import numpy as np
csv = np.genfromtxt ('file.csv', delimiter=",")
second = csv[:,1]
third = csv[:,2]

>>> second
Out[1]: array([ 432.,  300.,  432.])

>>> third
Out[2]: array([ 1.,  1.,  0.])

这篇关于如何在python中将CSV文件导入为numpy.array?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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