python csv根据另一列添加列值

时间:2022-11-09 13:08:15

I have a two column csv file

我有一个两列csv文件

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

I'm trying to write a python script so that each vote is counted depending on the row number, thus outputting

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

   row    vote
    1      1
    2      0
    3      1

what I've tried so far with a text file:

到目前为止我用文本文件尝试了什么:

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

and I'm getting IndexError: list index out of range errors

我得到IndexError:列表索引超出范围错误

1 个解决方案

#1


As @Adalee mentioned, you probably should have row = int(tokens[0]).

正如@Adalee所提到的,你可能应该有row = int(tokens [0])。

Here is one way to do this:

这是一种方法:

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

And output could be:

输出可能是:

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

test.csv file:

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

#1


As @Adalee mentioned, you probably should have row = int(tokens[0]).

正如@Adalee所提到的,你可能应该有row = int(tokens [0])。

Here is one way to do this:

这是一种方法:

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

And output could be:

输出可能是:

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

test.csv file:

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