使用 python 在 csv 文件中以特定方式添加列值

发布于 2024-09-09 01:19:55 字数 591 浏览 3 评论 0原文

我有一个类似于以下内容的 csv 文件:

title  title2  h1  h2  h3 ... 
l1.1     l1     1   1   0  
l1.2     l1     0   1   0
l1.3     l1     1   0   1
l2.1     l2     0   0   1
l2.2     l2     1   0   1
l3.1     l3     0   1   1
l3.2     l3     1   1   0
l3.3     l3     1   1   0
l3.4     l3     1   1   0    

我希望能够通过以下方式添加列:
h1 ( l1.1 + l1.2+ l1.3 ) = 2
h1 ( l2.1 + l2.2 ) = 1
h1 ( l3.1 + l3.2 + l3.3 +l3.4) = 3 每列依此类推 我想要每个这样的值的最终计数作为汇总表:

title2  h1  h2  h3...
l1     2   2   1
l2     1   0   2
l3     3   4   1

我如何实现这个?

i have a csv file similar to the following :

title  title2  h1  h2  h3 ... 
l1.1     l1     1   1   0  
l1.2     l1     0   1   0
l1.3     l1     1   0   1
l2.1     l2     0   0   1
l2.2     l2     1   0   1
l3.1     l3     0   1   1
l3.2     l3     1   1   0
l3.3     l3     1   1   0
l3.4     l3     1   1   0    

i want to be able to add the columns in the following manner:
h1 ( l1.1 + l1.2+ l1.3 ) = 2
h1 ( l2.1 + l2.2 ) = 1
h1 ( l3.1 + l3.2 + l3.3 +l3.4) = 3 and so on for every column
And i want the final count for every such value as a summarised table :

title2  h1  h2  h3...
l1     2   2   1
l2     1   0   2
l3     3   4   1

how do i implement this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幸福不弃 2024-09-16 01:19:55

像这样的东西应该有效。它接受表单中的输入

title,title2,h1,h2,h3
l1.1,l1,1,1,0
l1.2,l1,0,1,0
l1.3,l1,1,0,1
l2.1,l2,0,0,1
l2.2,l2,1,0,1
l3.1,l3,0,1,1
l3.2,l3,1,1,0
l3.3,l3,1,1,0
l3.4,l3,1,1,0

并输出

title2,h1,h2,h3
l1,2,2,1
l2,1,0,2
l3,3,4,1

Tested with Python 3.1.2。在 Python 2.x 中,您需要更改 open() 调用以使用二进制模式,并删除 newline="" 位。您还可以删除对 list() 的调用,因为在 Python 2.x 中,map() 已经返回一个列表。

import csv
import operator

reader = csv.reader(open("test.csv", newline=""), dialect="excel")
result = {}

for pos, entry in enumerate(reader):
    if pos == 0:
        headers = entry
    else:
        if entry[1] in result:
            result[entry[1]] = list(map(operator.add, result[entry[1]], [int(i) for i in entry[2:]]))
        else:
            result[entry[1]] = [int(i) for i in entry[2:]]

writer = csv.writer(open("output.txt", "w", newline=""), dialect="excel")
writer.writerow(headers[1:])

keys = sorted(result.keys())
for key in keys:
    output = [key]
    output.extend(result[key])
    writer.writerow(output)

Something like this should work. It takes an input in the form

title,title2,h1,h2,h3
l1.1,l1,1,1,0
l1.2,l1,0,1,0
l1.3,l1,1,0,1
l2.1,l2,0,0,1
l2.2,l2,1,0,1
l3.1,l3,0,1,1
l3.2,l3,1,1,0
l3.3,l3,1,1,0
l3.4,l3,1,1,0

and outputs

title2,h1,h2,h3
l1,2,2,1
l2,1,0,2
l3,3,4,1

Tested with Python 3.1.2. In Python 2.x you'll need to change the open() calls to use binary mode, and drop the newline="" bit). You can also drop the call to list() since in Python 2.x, map() already returns a list.

import csv
import operator

reader = csv.reader(open("test.csv", newline=""), dialect="excel")
result = {}

for pos, entry in enumerate(reader):
    if pos == 0:
        headers = entry
    else:
        if entry[1] in result:
            result[entry[1]] = list(map(operator.add, result[entry[1]], [int(i) for i in entry[2:]]))
        else:
            result[entry[1]] = [int(i) for i in entry[2:]]

writer = csv.writer(open("output.txt", "w", newline=""), dialect="excel")
writer.writerow(headers[1:])

keys = sorted(result.keys())
for key in keys:
    output = [key]
    output.extend(result[key])
    writer.writerow(output)
梦罢 2024-09-16 01:19:55

看一下 csv 模块。您想要做的是使用 csv.reader 打开文件。然后,您迭代该文件,一次一行。您将添加的结果累积到临时列表中。完成后,您可以将此列表写入新的 csv.writer。

您可能需要定义一种方言,因为您实际上并没有使用 CSV,而是使用某种制表符分隔的格式。

Have a look at the csv module. What you want to do is open the file with a csv.reader. Then you iterate over the file, one row at the time. You accumulate the results of the additions into a temporary list. When you are done, you write this list to a new csv.writer.

You might need to define a dialect as you are not really using CSV but some tab-delimited format.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文