将我的字典变成 pandas 数据框 [英] Turning my dictionary into a pandas dataframe
问题描述
我有一个函数,可以根据某些条件创建多个命令.
I have a function which create several dicts of dicts, based on some conditions.
但是,我真的很想在收集字典后将其转换为数据框. 但是我找不到一种简单的方法...现在,我在想解决方案是将字典中的每个键乘以最内部字典中键的数量,但是希望有更好的方法
However, I would really like to turn the dict into a dataframe after collecting it. But I can't find an easy way to do so... Right now I'm thinking the solution is to multiply each key in the dict by the number of keys in the most inner dict, but hopefully there is a better way
由于我的函数创建了字典,因此,如果有更好的方法可以更改它.
Since my function creates the dict I can alter it in any way, if there is a better way to do this.
这是我的字典
{'TSLA': {2011: {'negative': {'lowPrice': 185.16,
'lowDate': '05/27/19',
'highPrice': 365.71,
'highDate': '12/10/18',
'change': -0.49}},
2012: {'negative': {'lowPrice': 185.16,
'lowDate': '05/27/19',
'highPrice': 365.71,
'highDate': '12/10/18',
'change': -0.49}},
2013: {'negative': {'lowPrice': 32.91,
'lowDate': '01/07/13',
'highPrice': 37.24,
'highDate': '03/26/12',
'change': -0.12},
'positive': {'lowPrice': 32.91,
'lowDate': '01/07/13',
'highPrice': 190.9,
'highDate': '09/23/13',
'change': 4.8}}}}
我想要的输出将是这样的,当然带有值:
My desired output would be something like this, of course with the values:
lowPrice lowDate highPrice highDate change
ATVI 2012 Negative NaN NaN NaN NaN NaN
Positive NaN NaN NaN NaN NaN
2013 Negative NaN NaN NaN NaN NaN
TSLA 2014 Positive NaN NaN NaN NaN NaN
2012 Negative NaN NaN NaN NaN NaN
2013 Positive NaN NaN NaN NaN NaN
2014 Positive NaN NaN NaN NaN NaN
推荐答案
You can flatten nested dictionaries 2 times for tuples for keys and pass to DataFrame.from_dict
:
d1 = {(k1, k2, k3): v3
for k1, v1 in d.items()
for k2, v2 in v1.items()
for k3, v3 in v2.items()}
df = pd.DataFrame.from_dict(d1, orient='index')
#alternative
#df = pd.DataFrame(d1).T
print (df)
lowPrice lowDate highPrice highDate change
TSLA 2011 negative 185.16 05/27/19 365.71 12/10/18 -0.49
2012 negative 185.16 05/27/19 365.71 12/10/18 -0.49
2013 negative 32.91 01/07/13 37.24 03/26/12 -0.12
positive 32.91 01/07/13 190.9 09/23/13 4.8
这篇关于将我的字典变成 pandas 数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!