简体   繁体   English

转置Python字典的各个元素

[英]Transposing individual elements of a Python Dictionary

I have a python super dictionary that is made of individual dictionaries and currently looks like this: 我有一个由单个字典组成的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

I would like to transpose the individual dictionaries in my top_dictionary in a way such that all the value fields get transposed to the Value Column and the date is appended as a row item. 我想以某种方式对top_dictionary中的各个字典进行转置,以使所有值字段都转置到“值列”中,并且将日期作为行项附加。 The column name of the value field becomes a row for the Description column. 值字段的列名称将成为“描述”列的一行。

As an example the value in top_dictionary with the key: 'raw_data1' will then look like: 例如,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']}

I know how to do this for each individual dictionary by using the pandas melt function but how do I do this for my top_dictionary such that all elements inside it get transposed accordingly? 我知道如何使用pandasmelt函数为每个单独的字典执行此操作,但是如何为我的top_dictionary执行此操作,以使其中的所有元素都相应地转置?

Just loop over the top dictionary items. 只需循环浏览最重要的词典项目即可。 You'll have to play with if you want strict chronological order: 如果您希望按严格的时间顺序排列,则必须玩:

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')

Edit: This yields: 编辑:这产生:

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]}

Check the "Series_Date" output... if you don't want it sorted, remove the sorted method in the function. 检查“ Series_Date”输出...如果您不希望对其进行排序,请在函数中删除已排序的方法。 I only sorted it to match your specified output. 我只对它进行排序以匹配您指定的输出。

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()}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM