将csv加载到带有numpy的2D矩阵中进行绘图

时间:2010-11-30 15:40:23

标签: python arrays csv numpy reshape

鉴于此CSV文件:

"A","B","C","D","E","F","timestamp"
611.88243,9089.5601,5133.0,864.07514,1715.37476,765.22777,1.291111964948E12
611.88243,9089.5601,5133.0,864.07514,1715.37476,765.22777,1.291113113366E12
611.88243,9089.5601,5133.0,864.07514,1715.37476,765.22777,1.291120650486E12

我只想将其加载为3行7列的矩阵/ ndarray。但是,出于某种原因,我可以摆脱numpy是一个有3行(每行一个)而没有列的ndarray。

r = np.genfromtxt(fname,delimiter=',',dtype=None, names=True)
print r
print r.shape

[ (611.88243, 9089.5601000000006, 5133.0, 864.07514000000003, 1715.3747599999999, 765.22776999999996, 1291111964948.0)
 (611.88243, 9089.5601000000006, 5133.0, 864.07514000000003, 1715.3747599999999, 765.22776999999996, 1291113113366.0)
 (611.88243, 9089.5601000000006, 5133.0, 864.07514000000003, 1715.3747599999999, 765.22776999999996, 1291120650486.0)]
(3,)

我可以手动迭代并将其破解成我想要的形状,但这看起来很傻。我只是想把它作为一个合适的矩阵加载,这样我就可以将它切成不同的尺寸并绘制它,就像在matlab中一样。

3 个答案:

答案 0 :(得分:132)

纯粹的numpy

numpy.loadtxt(open("test.csv", "rb"), delimiter=",", skiprows=1)

查看loadtxt文档。

你也可以使用python的csv模块:

import csv
import numpy
reader = csv.reader(open("test.csv", "rb"), delimiter=",")
x = list(reader)
result = numpy.array(x).astype("float")

您必须将其转换为您喜欢的数字类型。我想你可以把整个事情写成一行:

result = numpy.array(list(csv.reader(open("test.csv", "rb"), delimiter=","))).astype("float")

添加提示:

您还可以使用pandas.io.parsers.read_csv并获取相关的numpy数组,该数组可以更快。

答案 1 :(得分:6)

我认为使用名称行的dtype会使例程混乱。尝试

>>> r = np.genfromtxt(fname, delimiter=',', names=True)
>>> r
array([[  6.11882430e+02,   9.08956010e+03,   5.13300000e+03,
          8.64075140e+02,   1.71537476e+03,   7.65227770e+02,
          1.29111196e+12],
       [  6.11882430e+02,   9.08956010e+03,   5.13300000e+03,
          8.64075140e+02,   1.71537476e+03,   7.65227770e+02,
          1.29111311e+12],
       [  6.11882430e+02,   9.08956010e+03,   5.13300000e+03,
          8.64075140e+02,   1.71537476e+03,   7.65227770e+02,
          1.29112065e+12]])
>>> r[:,0]    # Slice 0'th column
array([ 611.88243,  611.88243,  611.88243])

答案 2 :(得分:4)

您可以将包含标题的CSV文件读取到NumPy structured array np.genfromtxt。例如:

import numpy as np

csv_fname = 'file.csv'
with open(csv_fname, 'w') as fp:
    fp.write("""\
"A","B","C","D","E","F","timestamp"
611.88243,9089.5601,5133.0,864.07514,1715.37476,765.22777,1.291111964948E12
611.88243,9089.5601,5133.0,864.07514,1715.37476,765.22777,1.291113113366E12
611.88243,9089.5601,5133.0,864.07514,1715.37476,765.22777,1.291120650486E12
""")

# Read the CSV file into a Numpy record array
r = np.genfromtxt(csv_fname, delimiter=',', names=True, case_sensitive=True)
print(repr(r))

看起来像这样:

array([(611.88243, 9089.5601, 5133., 864.07514, 1715.37476, 765.22777, 1.29111196e+12),
       (611.88243, 9089.5601, 5133., 864.07514, 1715.37476, 765.22777, 1.29111311e+12),
       (611.88243, 9089.5601, 5133., 864.07514, 1715.37476, 765.22777, 1.29112065e+12)],
      dtype=[('A', '<f8'), ('B', '<f8'), ('C', '<f8'), ('D', '<f8'), ('E', '<f8'), ('F', '<f8'), ('timestamp', '<f8')])

您可以访问像r['E']这样的命名列:

array([1715.37476, 1715.37476, 1715.37476])

注意:此答案之前使用np.recfromcsv将数据读入NumPy record array。虽然该方法没有任何问题,但结构化数组通常比记录数组更好,以提高速度和兼容性。