我有一个由单个词典组成的python超级词典,目前看起来像这样:
raw_data1 = {'Series_Date':['2017-03-10','2017-03-13','2017-03-14','2017-03-15'],'SP':[35.6,56.7,41,41],'1M':[-7.8,56,56,-3.4],'3M':[24,-31,53,5]}
raw_data2 = {'Series_Date':['2017-03-10','2017-03-13','2017-03-14','2017-03-15'],'SP':[35.6,56.7,41,41],'1M':[-7.8,56,56,-3.4],'3M':[24,-31,53,5]}
raw_data3 = {'Series_Date':['2017-03-10','2017-03-13','2017-03-14','2017-03-15'],'SP':[35.6,56.7,41,41],'1M':[-7.8,56,56,-3.4],'3M':[24,-31,53,5]}
top_dictionary = {
'raw_data1': raw_data1,
'raw_data2': raw_data2,
'raw_data3': raw_data3
}
print top_dictionary
我想在我的top_dictionary中转换单个词典,使得所有值字段都转换为值列,并将日期作为行项追加。值字段的列名称将成为“描述”列的行。
作为示例,top_dictionary中的值包含键:' raw_data1'然后看起来像:
raw_data1 = {'Series_Date':['2017-03-10','2017-03-10','2017-03-10','2017-03-13','2017-03-13','2017-03-13','2017-03-14','2017-03-14','2017-03-14','2017-03-15','2017-03-15','2017-03-15'],'Value':[35.6,-7.8,24,56.7,56,-31,41,56,53,41,-3.4,5],'Desc':['SP','1M','3M','SP','1M','3M','SP','1M','3M','SP','1M','3M']}
我知道如何通过使用pandas融合功能为每个单独的字典执行此操作,但是如何为top_dictionary执行此操作以使其中的所有元素都相应地进行转置?
答案 0 :(得分:0)
只需遍历顶级字典项。如果你想要严格的时间顺序,你必须要玩:
top_dict_new = dict()
for key, data_dict in top_dictionary.items():
df = pd.melt(pd.DataFrame(data_dict), id_vars = ['Series_Date'])
top_dict_new[key] = df.to_dict('list')
编辑: 这会产生:
print top_dict_new['raw_data1']
{'variable': ['1M', '1M', '1M', '1M', '3M', '3M', '3M', '3M', 'SP', 'SP', 'SP', 'SP'], 'Series_Date': ['2017-03-10', '2017-03-13', '2017-03-14', '2017-03-15', '2017-03-10', '2017-03-13', '2017-03-14', '2017-03-15', '2017-03-10', '2017-03-13', '2017-03-14', '2017-03-15'], 'value': [-7.7999999999999998, 56.0, 56.0, -3.3999999999999999, 24.0, -31.0, 53.0, 5.0, 35.600000000000001, 56.700000000000003, 41.0, 41.0]}
答案 1 :(得分:0)
检查“Series_Date”输出...如果您不想对其进行排序,请删除该函数中的已排序方法。我只对它进行了排序以匹配您指定的输出。
def make_dict(dict_in, val_cols):
d_out = {}
d_out['Value'] = pd.DataFrame(dict_in)[val_cols].values.flatten()
d_out['Series_Date'] = sorted(dict_in['Series_Date'] * len(val_cols))
d_out['Desc'] = val_cols * len(val_cols)
return d_out
new_dict = {k: make_dict(v, ['SP','1M', '3M']) for k, v in top_dictionary.items()}