繁体   English   中英

python csv基于另一列添加列值

[英]python csv add column values based on another column

我有两列的csv文件

    row    vote
     1      0
     1      0
     1      1
     2      0
     2      0
     3      1
     3      0

我正在尝试编写一个python脚本,以便根据行号对每个投票进行计数,从而输出

   row    vote
    1      1
    2      0
    3      1

到目前为止,我已经尝试过使用文本文件进行以下操作:

from collections import defaultdict

d = defaultdict(int)

with open("data.txt") as f:
    for line in f:
        tokens = [t.strip() for t in line.split(",")]
        try:
            row = int(tokens[1])
            vote = int(tokens[1])
        except ValueError:
            continue
        d[row] += vote
print d

而且我得到IndexError: list index out of range错误

如@Adalee所述,您可能应该具有row = int(tokens[0])

这是执行此操作的一种方法:

result = {}

with open("test.csv") as f:
    for line in f:
        tokens = line.split(",")

        row = None
        vote = None

        try:
            row = int(tokens[0])
            vote = int(tokens[1])
        except Exception as e:
            pass

        if row is not None:
            if result.has_key(row):
                result[row] += vote
            else:
                result[row] = vote

print result

输出可能是:

{1: 1, 2: 3, 3: 9}

test.csv文件:

row,vote
1,0
1,0
1,1
2,2
2,1
3,4
3,2
3,3

暂无
暂无

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

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