简体   繁体   English

将csv文件存储在数组中,删除第一列[Python]

[英]Store csv file in array removing first column [Python]

I want to store a csv file in an array in python removing the first column. 我想在删除第一列的python中将csv文件存储在数组中。 In order to store it I just write these 2 lines of code 为了存储它,我只编写了这两行代码

from numpy import genfromtxt
def csvToArray():
    data = genfromtxt('SP500Index.csv', delimiter=',')
    return array

and it worked. 而且有效。 From this point how can I get rid of the first column? 从这一点上,我如何摆脱第一列?

Just use the numpy.delete function. 只需使用numpy.delete函数。 Add it to the code as follows: 如下所示将其添加到代码中:

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's delete documentation is here . Numpy的delete文档在此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM