我想在python中的数组中存储一个csv文件,删除第一列。为了存储它,我只写了这两行代码
from numpy import genfromtxt
def csvToArray():
data = genfromtxt('SP500Index.csv', delimiter=',')
return array
它有效。从这一点开始,如何摆脱第一列?
答案 0 :(得分:1)
只需使用numpy.delete函数即可。将其添加到代码中,如下所示:
import numpy as np
def csvToArray():
data = np.genfromtxt('SP500Index.csv', delimiter=',')
data = np.delete(data, 0, 1) # the first input is the array, second is
# which row/column to delete, third
# determines if it is row (0) or column (1)
return data #not sure why it had array before
Numpy的delete
文档为here。