我有一个代码,可以计算几个指标,这些指标存储为字典的三维词典。我想将这个词典打印成一个csv文件 - 但是没有找到一个很好的方法。
一旦计算了字典中的所有元素,我想将其打印到文件(其中periods
是文件的标题,keys
和指标a, b, and c
应该是列 - 列键和列指标。)
有没有简单的方法将其打印到文件? (我的第一次尝试是熊猫,但这不起作用)
由于
from collections import defaultdict
import pandas as pd
import os
import random
# 3 dimensional dictionary that stores integers
output_dict = defaultdict(lambda: defaultdict(lambda: defaultdict(int)))
# Array of periods
periods = range(0, 2)
# relevant keys
keys = ["key1", "key2"]
# Iterate over all periods
for period in periods:
# Iterate over all relevant keys
for key in keys:
# Store results for key for each time period for each category ("a", "b", or "c")
output_dict[key][period]["a"] += random.randint(1, 1000)
output_dict[key][period]["b"] += random.randint(1, 1000)
output_dict[key][period]["c"] += random.randint(1, 1000)
# This is the tricky part!!!
# Store results
pd.DataFrame(output_dict).to_csv("output_dict.csv", index=False)
# the dictionary may look as follows:
output_dict = {"key1": {0: {"a": 0.9, "b": 0.2, "c": 0.5}, 1:{"a": 0.91, "b": 0.3, "c": 0.4}},
"key2": {0: {"a": 0.4, "b": 0.33, "c": 0.34}, 1: {"a": 0.21, "b": 0.73, "c": 0.54}}}
答案 0 :(得分:1)
您应该只使用csv
模块,我认为值得对您的数据进行争论,以使其与pandas
DataFrame构造函数完美匹配。注意,我正在将csv写入字符串i / o缓冲区而不是文件,因此我可以轻松地打印结果,但您可以简单地省略这些内容并使用普通文件对象。
>>> periods = [0, 1]
>>> metrics = ['a', 'b', 'c']
>>> import csv
>>> import io
现在,请仔细构建您的行:
>>> with io.StringIO() as f:
... writer = csv.writer(f)
... writer.writerow(['Key','Metric', 0, 1])
... for key in output_dict:
... for metric in metrics:
... row = [key, metric]
... for p in periods:
... row.append(output_dict[key][p][metric])
... writer.writerow(row)
... final = f.getvalue()
...
16
17
18
18
17
16
16
>>> print(final)
Key,Metric,0,1
key2,a,0.4,0.21
key2,b,0.33,0.73
key2,c,0.34,0.54
key1,a,0.9,0.91
key1,b,0.2,0.3
key1,c,0.5,0.4
请注意,密钥不会按任何特定顺序排列,因为字典是无序的。如果您提前了解所有密钥,则可以通过迭代所有密钥来强制执行订单,就像我对指标和期间所做的那样(您的问题暗示 提前知道)。可以扩展此解决方案,以便轻松处理丢失的密钥。
修改强>: 您的上一次编辑似乎意味着您将提前知道密钥,所以只需执行以下操作:
>>> periods = [0, 1]
>>> keys = ['key1', 'key2']
>>> metrics = ['a', 'b', 'c']
>>> with io.StringIO() as f:
... writer = csv.writer(f)
... writer.writerow(['Key','Metric', 0, 1])
... for key in keys:
... for metric in metrics:
... row = [key, metric]
... for p in periods:
... row.append(output_dict[key][p][metric])
... writer.writerow(row)
... final = f.getvalue()
...
16
17
16
16
17
18
18
>>> print(final)
Key,Metric,0,1
key1,a,0.9,0.91
key1,b,0.2,0.3
key1,c,0.5,0.4
key2,a,0.4,0.21
key2,b,0.33,0.73
key2,c,0.34,0.54