我正在尝试以以下格式打印字典。我想这样做是因为我需要使用JSON格式绘制D3中的一些趋势。对于这种趋势,我计算了每个州(1980年代至2010年代)在每个州的谋杀案数量。
我能够输出文件和所有内容,但是由于我试图创建图形,因此在标记文件中字典中的每个键,值对方面,JSON文件中的数据格式必须非常具体。输出。
xl = pd.ExcelFile('Wyoming.xlsx')
df = xl.parse('Sheet1')
year = df['Year']
state = df['State']
freq = dict()
for i in range(0, len(df)):
currYear = year.iloc[i]
if(currYear >= 1980 and currYear < 1989):
currDecade = 1980
elif(currYear >= 1990 and currYear < 2000):
currDecade = 1990
elif(currYear >= 2000 and currYear < 2010):
currDecade = 2000
elif(currYear >= 2010):
currDecade = 2010
currState = state.iloc[i]
if currDecade in freq:
if currState in freq[currDecade]:
freq[currDecade][currState] += 1
else:
key = {currState: 1}
freq[currDecade].update(key)
else:
key = {currDecade:{currState: 1}}
freq.update(key)
#print(freq)
freq1 = [{'Decade': d, 'State': [{'State': s, 'Freq': f}]} for d, s, f in freq.items()]
print(freq1)
我收到错误消息“ ValueError:没有足够的值要解压(预期3,得到2)”
我希望输出如下。
[{"Decade": "1980", "State": [{"State": "California", "Freq": 29591}, {"State": "Massachusetts", "Freq": 1742}, ...}]
答案 0 :(得分:1)
罪魁祸首是for d, s, f in freq.items()
,因为freq.items()
以频率返回(key, value)
对中的可迭代对象。由于您已嵌套字典,请尝试以下操作:
freq1 = [{'Decade': d, 'State': [{'State': s, 'Freq': f} for s, f in sdict.items()]}
for d, sdict in freq.items()
]
答案 1 :(得分:1)
dict.items()
仅使用两个元素来迭代元组:键和值。
freq1 = []
for decade, states in freq.items():
freq1.append({
'Decade': decade,
'State': []
})
for state, freq in states.items():
freq1['State'].append([{'State': state, 'Freq': freq}])
print(freq1)
我认为这种方式使代码更具可读性。但是,如果您仍然喜欢单行列表理解解决方案,则为:
freq1 = [{'Decade': d, 'State': [{'State': s, 'Freq': f} for s, f in ss.items()]} for d, ss in freq.items()]