如何将 CSV 加载到 IPython 笔记本中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24853632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to load a CSV into IPython notebook
提问by
I have a csv file ready to load into my python code, however, I want to load it into the following format:
我有一个 csv 文件准备加载到我的 python 代码中,但是,我想将它加载为以下格式:
data = [[A,B,C,D],
[A,B,C,D],
[A,B,C,D],
]
How would I go about loading a .csv file that is readable as a numpy array? e.g., simply using previous tutorials plays havoc with using:
我将如何加载可作为 numpy 数组读取的 .csv 文件?例如,简单地使用以前的教程会破坏使用:
data = np.array(data)
Failing that, I would just like to upload my csv file (e.g. 'dual-Cored.csv' as data = dual-Cored.csv)
如果失败,我只想上传我的 csv 文件(例如“dual-Cored.csv”作为数据 = dual-Cored.csv)
采纳答案by DrV
The simplest solution is just:
最简单的解决方案是:
import numpy as np
data = np.loadtxt("myfile.csv")
As long as the data is convertible into float
and has an equal number of columns on each row, this works.
只要数据可以转换为float
并且每行具有相同数量的列,这就是有效的。
If the data is not convertible into float
in some column, you may write your own converters for it. Please see the numpy.loadtxt
documentation. It is really very flexible.
如果数据float
在某些列中无法转换,您可以为它编写自己的转换器。请参阅numpy.loadtxt
文档。它真的非常灵活。
回答by Cory Kramer
As a small example, I have some file data.csv
with the following contents.
作为一个小例子,我有一些data.csv
包含以下内容的文件。
A,B,C,D
1,2,3,4
W,X,Y,Z
5,6,7,8
A,B,C,D
1,2,3,4
W,X,Y,Z
5,6,7,8
with open('data.csv', 'r') as f:
data = [i.split(",") for i in f.read().split()]
print data
Output
输出
[['A', 'B', 'C', 'D'],
['1', '2', '3', '4'],
['W', 'X', 'Y', 'Z'],
['5', '6', '7', '8']]
回答by unutbu
If your CVS looks like this:
如果您的 CVS 如下所示:
A,B,C,D
A,B,C,D
A,B,C,D
A,B,C,D
then
然后
import csv
with open(filename, 'rb') as f:
data = list(csv.reader(f))
would make data
equal to
将data
等于
[['A', 'B', 'C', 'D'],
['A', 'B', 'C', 'D'],
['A', 'B', 'C', 'D'],
['A', 'B', 'C', 'D']]
回答by Aaron Hall
I'm assuming you mean to get all your data points as integers or floating point numbers.
我假设您的意思是将所有数据点作为整数或浮点数。
First I wrote some sample data:
首先我写了一些示例数据:
with open('dual-Cored.csv', 'w') as f:
f.write('1,2,3,4\n5,6,7,8\n9,10,11,12')
Now I'm reading back in the sample data
现在我正在回读示例数据
with open('dual-Cored.csv', 'rU') as f:
c = csv.reader(f)
for l in c:
print list(map(int, l))
Which prints:
哪个打印:
[1, 2, 3, 4]
[5, 6, 7, 8]
[9, 10, 11, 12]
I recommend you read up a bit on datatypes in the Python tutorial, which talks about the difference between strings and numerical types.
我建议您在 Python 教程中阅读一些有关数据类型的内容,该教程讨论了字符串和数字类型之间的区别。
To read into a numpy array with the csv module:
要使用 csv 模块读入 numpy 数组:
import numpy
with open('dual-Cored.csv', 'rU') as f:
c = csv.reader(f)
ar = numpy.array(list(c), dtype=int)
and ar
now returns:
而ar
现在返回:
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
Or directly use the numpy.genfromtxt
function (you'll need to specify the delimiter):
或者直接使用该numpy.genfromtxt
函数(您需要指定分隔符):
numpy.genfromtxt('dual-Cored.csv', delimiter=',')
returns:
返回:
array([[ 1., 2., 3., 4.],
[ 5., 6., 7., 8.],
[ 9., 10., 11., 12.]])