gpt4 book ai didi

python - 使用 Python 创建一个交叉表(可能/有点,不确定它叫什么)

转载 作者:太空宇宙 更新时间:2023-11-04 09:07:49 25 4
gpt4 key购买 nike

我想要实现的是一个如下所示的 csv 文件:

,h.r9,h.r8,h.r5,h.r4,h.r7,h.r6,h.r1,h.r3,h.r2,h.r10
chris,0,1,0,0,0,0,0,1,0,0
bob,0,0,1,1,1,1,0,0,0,1
dick,1,0,1,0,0,0,0,0,1,1
ab,1,2,0,0,0,0,0,0,0,0
emma,0,0,1,0,1,0,1,0,1,0

有些无关:有人知道这样的矩阵的实际名称是什么吗?这至少会让谷歌搜索这个问题更容易。

数据原本在这样的字典中:

dict = {}
dict['ab'] = ["h.r1","h.r2"]
dict['bob'] = ["h.r3","h.r4","h.r5","h.r6","h.r10"]
dict['chris'] = ["h.r2","h.r8"]
dict['dick'] = ["h.r1","h.r10","h.r9","h.r3"]
dict['emma'] = ["h.r3","h.r5","h.r7","h.r9"]

因此,如果一个人在字典中有变量,则 CSV 中应该有一个 1,否则应该有一个零。

下面的代码构建了 csv 文件,但全为零。我将如何实现 1 和 0 的逻辑?我可以在 PHP/HTML 中执行此操作,但作为 Python 的新手,我并没有 catch ......还有一个库可能已经内置了这个功能吗? (也许是 numpy?)

import csv

dict = {}
dict['ab'] = ["h.r1","h.r2"]
dict['bob'] = ["h.r3","h.r4","h.r5","h.r6","h.r10"]
dict['chris'] = ["h.r2","h.r8"]
dict['dick'] = ["h.r1","h.r10","h.r9","h.r3"]
dict['emma'] = ["h.r3","h.r5","h.r7","h.r9"]

mergedlist = []
for k,v in dict.items():
mergedlist = mergedlist + v
mergedlist = list(set(mergedlist))
listlen = len(mergedlist)
mergedlist[0:0] = " "

thekeys = []
for k in dict.keys():
thekeys.append(k)

thecsv = csv.writer(open("your.csv", 'w', newline=''))
thecsv.writerow(mergedlist)

for col in thekeys:
thecsv.writerow([col] + ['0']*listlen) #does the logic go in here somehow??

最佳答案

我不知道大部分代码在做什么。只需几行即可完成:

all_values = set()
for person_values in data.values():
all_values = all_values.union(person_values)
all_values_list = list(all_values)

thecsv = csv.writer(open("your.csv", 'w'))
thecsv.writerow(all_values_list)
for name, values in data.items():
row = [1 if i in values else 0 for i in all_values_list]
thecsv.writerow([name] + row)

请注意,我已将您的字典重命名为 data,因为覆盖内置函数/类名称不是一个好主意。

关于python - 使用 Python 创建一个交叉表(可能/有点,不确定它叫什么),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18423101/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com