在Python中:创建一个表来可视化成对数据

时间:2012-12-12 01:33:51

标签: python

我进行了一系列成对比较(确切地说是241 x 241) 生成的文件如下所示:

A,X,10
A,Y,20
X,Y,15

我想将此转换为显示所有成对比较的表格。即像这样的东西

,A,X,Y
A,,10,20
X,10,,15
Y,20,15,,

我不知道如何开始解决这个问题。任何帮助,建议将不胜感激!

2 个答案:

答案 0 :(得分:2)

将配对数据存储到词典中:

result_dict = {
    (A, X): 10,
    (A, Y): 20,
    (X, Y): 15,
}

和行/列标签:

cols = sorted(set(a for a, b in result_dict.iterkeys()))
rows = sorted(set(b for a, b in result_dict.iterkeys()))

然后就是你打印行的方式......

for b in rows:
    row = list(result_dict.get((a, b), None) for a in cols)
    # print the row

答案 1 :(得分:1)

我觉得有更有效的方法,但你可以给这样的东西一个镜头。它使用csv模块加载/解析您的数据,然后将其写回csv(假设您希望输出在文件中,即 - 如果不是,则可以调整):

import csv
from collections import defaultdict

# Open the input file and read it all into a defaultdict
with open('table.csv', 'rb') as f:
  reader = csv.reader(f)

  # Dictionary to hold the nested values
  # This will be keyed by the letter ID, and each line of
  # the file will be written twice (so A,X,10 will be represented
  # as {'A': {'X': 10}, 'X': {'A': 10}}
  d = defaultdict(dict)
  for row in reader:
    d[row[0]][row[1]] = row[2]
    d[row[1]][row[0]] = row[2]

# Now we open the output file and write out our defaultdict
with open('t_out.csv', 'wb') as o:
  # Here our fieldnames will start with the 'empty' first header
  # and then be comprised of the keys of the dictionary (which
  # should contain all possible values for the table)
  fieldnames = [' '] + d.keys()
  writer = csv.DictWriter(o, fieldnames=fieldnames)

  # In Python 2.7, you can use writer.writeheaders()
  writer.writerow(dict((h, h) for h in fieldnames))

  # Now iterate through our dictionary, creating a row
  # dictionary that will contain the information to be written
  for k, v in d.iteritems():
    # Here we are putting the key in the 'empty' first column
    v[' '] = k
    writer.writerow(v)