对于具有匹配列A值的行,在列B中求和值

时间:2018-02-09 06:32:42

标签: python csv

大型csv的摘录如下所示:

Description,Foo,Excl,GST,Incl
A,foo,$154.52,$15.44,$169.96
A,foo,$45.44,$4.54,$49.98
A,foo,$45.44,$4.54,$49.98
A,foo,$154.52,$15.44,$169.96
A,foo,$0.00,$0.00,$0.00
A,foo,$50.16,$5.02,$55.18
B,foo,$175.33,$15.65,$190.98
C,foo,$204.52,$15.44,$219.96
D,foo,$154.52,$15.44,$169.96
D,foo,$154.52,$15.44,$169.96
D,foo,$45.44,$4.54,$49.98
D,foo,$154.52,$15.44,$169.96
D,foo,$145.44,$14.54,$159.98

我需要剥离美元符号以及包含匹配的Description值(AB或其他任何值)的所有行,并将Excl列值相加另外,GST列分别为Incl值和Description列值Description列值。

最终结果应该是包含Excl列作为键的字典对象,以及与GST匹配的InclDescription{ "A": [450.08,44.98,495.06], "B": [175.33,15.65,190.98], "C": [204.52,15.44,219.96], "D": [654.44,65.40,719.84] } 列的总和},例如:

import csv

def getField(rowdata, index):
    try:
        val = rowdata[index]
    except IndexError:
        val = '-1'
    return val

with open(csv, 'r') as f:
    reader = csv.reader(f)
    order_list = list(reader)

# Remove the header row in csv
order_list.pop(0)

for row in order_list:
    Desc = getField(row, 0)
    Excl = getField(row, 2)
    GST  = getField(row, 3)
    Incl = getField(row, 4)

我完全不知道如何执行总和操作。我的代码只能打开csv并读取每行的值。任何启蒙都表示赞赏。

{{1}}

1 个答案:

答案 0 :(得分:1)

这可能会有所帮助

import csv
import decimal

path = "Path to CSV_File.csv"

def removeSym(s):
    return float(s.replace("$", ""))

with open(path, 'r') as f:
    reader = csv.reader(f)
    order_list = list(reader)

d = {}
for i in order_list[1:]:    #Skip reading the first line
    if i[0] not in d:
        d[i[0]] = map(removeSym, i[2:])   #Check if desc is a key the result dict. if not create 
    else:
        d[i[0]] = [float(round(sum(k),2)) for k in zip(d[i[0]], map(removeSym, i[2:]))]

print d

<强>输出:

{'A': [450.08, 44.98, 495.06], 'C': [204.52, 15.44, 219.96], 'B': [175.33, 15.65, 190.98], 'D': [654.44, 65.4, 719.84]}