如何在python中减少矩阵列?

如何在python中减少矩阵列?,python,loops,csv,matrix,Python,Loops,Csv,Matrix,我有一个包含大量数据、22列和10000行的csv文件。 第一行是标题,所有其他行都是数据。 我从文件中读到。(请阅读,我不想更改原始文件) 现在我想减少col的数量,只保存3个col,按标题名保存它。col的顺序可以在不同的文件中更改,有时“LUX”col在第5列中,有时在第20列或第8列中,等等。。 到目前为止,我得到了这个: with open('test.csv', 'rb') as csvfile: spamreader = csv.reader(csvfile, delimiter=

我有一个包含大量数据、22列和10000行的csv文件。 第一行是标题,所有其他行都是数据。 我从文件中读到。(请阅读,我不想更改原始文件) 现在我想减少col的数量,只保存3个col,按标题名保存它。col的顺序可以在不同的文件中更改,有时“LUX”col在第5列中,有时在第20列或第8列中,等等。。 到目前为止,我得到了这个:

with open('test.csv', 'rb') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',', quotechar='|') # open the csv file

medianGoodPixels = [] #vector to pixels
Lux = [] #vector to LUX
sdVer = [] # vector to the version
NewCsvTable = [] #will be a matrix with 3 cols, LUX, pixels, and version

for row in spamreader:
    if row == "LUX": 
         #Here I'm stuck
我意识到像这样的行,将给出每个迭代中的所有行,所以在第二次迭代中,它将只是第二行的数据。 我需要以某种方式使用2个循环,我想,但不知道如何准确


谢谢。

您可以使用标题行上的
list.index
查找各种标题的索引

with open('test.csv', 'rb') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=',', quotechar='|') # open the csv file
    medianGoodPixels = [] #vector to pixels
    Lux = [] #vector to LUX
    sdVer = [] # vector to the version
    NewCsvTable = [] #will be a matrix with 3 cols, LUX, pixels, and version
    header = next(spamreader)  #Returns the header 
    lux_col, pixel_col, version_col = header.index('LUX'), header.index('pixel'),\
                                      header.index('version')

    #Now iterate over rest of the rows. 
    for row in spamreader:
        Lux.append(row[lux_col])
        sdVer.append(row[version_col])
        medianGoodPixels.append(row[pixel_col])  

这绝对是专门的
csv
模块类
csv.DictReader
的工作,它使用文档的第一行找出列名称,然后每行返回一个字典

例如:

Lux, sdVer, medianGoodPixels = [], [], []
with open('test.csv', 'rb') as csvfile:
    csv_reader = csv.DictReader(csvfile, delimiter=',', quotechar='|')
    for dict_row in csv_reader:
        Lux.append(dict_row['LUX'])
        sdVer.append(dict_row['version'])
        medianGoodPixel.append(dict_row['pixel'])