如何将CSV加载到IPython笔记本中 [英] How to load a CSV into IPython notebook
问题描述
我有一个csv文件已准备好加载到我的python代码中,但是,我想将其加载到以下格式:
I have a csv file ready to load into my python code, however, I want to load it into the following format:
data = [[A,B,C,D],
[A,B,C,D],
[A,B,C,D],
]
如何加载可读作为numpy数组的.csv文件?例如,简单地使用以前的教程会严重破坏使用:
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:
data = np.array(data)
如果不这样,我只想上传我的csv文件(例如'dual-Cored.csv'作为data = dual-Cored。 csv)
Failing that, I would just like to upload my csv file (e.g. 'dual-Cored.csv' as data = dual-Cored.csv)
推荐答案
最简单的解决方案就是:
The simplest solution is just:
import numpy as np
data = np.loadtxt("myfile.csv")
只要数据可以转换为 float
并且每行的列数相同,就可以了。
As long as the data is convertible into float
and has an equal number of columns on each row, this works.
如果某些列中的数据无法转换为 float
,您可以为其编写自己的转换器。请参阅 numpy.loadtxt
文档。它非常灵活。
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.
这篇关于如何将CSV加载到IPython笔记本中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!