Python比较2个CSV-平均2列

时间:2018-12-12 02:00:48

标签: python

我想获取2个csv文件中a列的平均值

import csv

master = open('master.csv', 'r')
export = open('export.csv', 'r')

#output
outFile = open ('out.csv', 'w')
csvWriter = csv.writer(outFile, delimiter=',')

# Process input files
csv1 = csv.reader(master, delimiter=',')
csv2 = csv.reader(export, delimiter=',')

# Skip headers
next(csv1)
next(csv2)

for rowExport in csv2:
    # Get each line in MasterList
    rowMaster = next(csv1)

    #output line from Export values
    colA = str(rowMaster[1])
    colB = str(rowExport[5])

    #Average colB is the average of 2 same column in master and export (in master it is in column 2 while column 5 in export)
    if float(rowExport[5])*2 > float(rowMaster[2]):
        colB = str((float(rowExport[5])+float(rowMaster[2])/2))    
    csvWriter.writerow([colA, colB])                                                                                     

master.close()
export.close()
outFile.close()

我只想基于主CSV中的客户ID来获取“文档排队”列的平均值。查看所附图片。

Export Master

我得到的是将平均数基于行,它与客户ID不匹配。由于它是随机排序的 Result

0 个答案:

没有答案