我有一个嵌套字典。这是一些纳斯达克数据。像这样:
{'CLSN':
Date Open High Low Close Volume Adj Close
2015-12-31 1.92 1.99 1.87 1.92 79600 1.92
2016-01-04 1.93 1.99 1.87 1.93 39700 1.93
2016-01-05 1.89 1.94 1.85 1.90 50200 1.90,
'CCC':
Date Open High Low Close Volume Adj Close
2015-12-31 17.270000 17.389999 17.120001 17.250000 177200 16.965361
2016-01-04 17.000000 17.219999 16.600000 17.180000 371600 16.896516
2016-01-05 17.190001 17.530001 17.059999 17.450001 417500 17.162061,
}
为了帮助您理解,它是 键 后跟 值 ,而 值 是 数据帧 !
在询问之前,我尝试了
pd.Panel(nas)['CLSN']
的方式,所以我确定它的值是一个数据帧。但是 pd.Panel(nas).to_frame().reset_index()
的方式根本没有帮助我!它输出一个空数据框,其中包含数千个由股票名称填充的列。现在很烦人,我想要一个这样的数据框:
index Date Open High Low Close Volume Adj Close CLSN 2015-12-31 1.92 1.99 1.87 1.92 79600.0 1.92
CLSN 2016-01-01 NaN NaN NaN NaN NaN NaN
ClSN 2016-01-04 1.93 1.99 1.87 1.93 39700.0 1.93
CCC 2015-12-31 17.270000 17.389999 17.120001 17.250000 177200.0 16.965361
CCC 2016-01-04 17.000000 17.219999 16.600000 17.180000 371600.0 16.896516
CCC 2016-01-05 17.190001 17.530001 17.059999 17.450001 417500.0 17.162061
当然,我可以使用
for
循环来获取每只股票的数据框,但是我无法加入它们。你有更好的主意吗?非常愿意知道!
对 MaxU:
使用
print(nas['CLSN'].head())
方法后,它输出如下: Open High Low Close Volume Adj Close
Date
2015-12-31 1.92 1.99 1.87 1.92 79600 1.92
2016-01-04 1.93 1.99 1.87 1.93 39700 1.93
2016-01-05 1.89 1.94 1.85 1.90 50200 1.90
2016-01-06 1.86 1.89 1.77 1.78 62100 1.78
2016-01-07 1.75 1.80 1.75 1.77 117000 1.77
最佳答案
更新:
假设 Date
是一个索引(不是常规列):
源字典:
In [70]: d2
Out[70]:
{'CCC': Open High Low Close Volume Adj Close
Date
2015-12-31 17.270000 17.389999 17.120001 17.250000 177200 16.965361
2016-01-04 17.000000 17.219999 16.600000 17.180000 371600 16.896516
2016-01-05 17.190001 17.530001 17.059999 17.450001 417500 17.162061,
'CLSN': Open High Low Close Volume Adj Close
Date
2015-12-31 1.92 1.99 1.87 1.92 79600 1.92
2016-01-04 1.93 1.99 1.87 1.93 39700 1.93
2016-01-05 1.89 1.94 1.85 1.90 50200 1.90}
解决方案:
In [73]: pd.Panel(d2).swapaxes(0, 2).to_frame().reset_index(level=0).sort_index()
Out[73]:
Date Open High Low Close Volume Adj Close
minor
CCC 2015-12-31 17.270000 17.389999 17.120001 17.250000 177200.0 16.965361
CCC 2016-01-04 17.000000 17.219999 16.600000 17.180000 371600.0 16.896516
CCC 2016-01-05 17.190001 17.530001 17.059999 17.450001 417500.0 17.162061
CLSN 2015-12-31 1.920000 1.990000 1.870000 1.920000 79600.0 1.920000
CLSN 2016-01-04 1.930000 1.990000 1.870000 1.930000 39700.0 1.930000
CLSN 2016-01-05 1.890000 1.940000 1.850000 1.900000 50200.0 1.900000
或者,您可以将
Date
作为索引的一部分:In [74]: pd.Panel(d2).swapaxes(0, 2).to_frame().sort_index()
Out[74]:
Open High Low Close Volume Adj Close
Date minor
2015-12-31 CCC 17.270000 17.389999 17.120001 17.250000 177200.0 16.965361
CLSN 1.920000 1.990000 1.870000 1.920000 79600.0 1.920000
2016-01-04 CCC 17.000000 17.219999 16.600000 17.180000 371600.0 16.896516
CLSN 1.930000 1.990000 1.870000 1.930000 39700.0 1.930000
2016-01-05 CCC 17.190001 17.530001 17.059999 17.450001 417500.0 17.162061
CLSN 1.890000 1.940000 1.850000 1.900000 50200.0 1.900000
旧答案 - 它假定
Date
是一个常规列(不是索引)试试这个:
In [59]: pd.Panel(d).swapaxes(0, 2).to_frame().reset_index('major', drop=True).sort_index()
Out[59]:
Date Open High Low Close Volume Adj Close
minor
CCC 2015-12-31 17.27 17.39 17.12 17.25 177200 16.9654
CCC 2016-01-04 17 17.22 16.6 17.18 371600 16.8965
CCC 2016-01-05 17.19 17.53 17.06 17.45 417500 17.1621
CLSN 2015-12-31 1.92 1.99 1.87 1.92 79600 1.92
CLSN 2016-01-04 1.93 1.99 1.87 1.93 39700 1.93
CLSN 2016-01-05 1.89 1.94 1.85 1.9 50200 1.9
其中
d
是您的 nested dictionary
:In [60]: d
Out[60]:
{'CCC': Date Open High Low Close Volume Adj Close
0 2015-12-31 17.270000 17.389999 17.120001 17.250000 177200 16.965361
1 2016-01-04 17.000000 17.219999 16.600000 17.180000 371600 16.896516
2 2016-01-05 17.190001 17.530001 17.059999 17.450001 417500 17.162061,
'CLSN': Date Open High Low Close Volume Adj Close
0 2015-12-31 1.92 1.99 1.87 1.92 79600 1.92
1 2016-01-04 1.93 1.99 1.87 1.93 39700 1.93
2 2016-01-05 1.89 1.94 1.85 1.90 50200 1.90}
关于python - 如何将嵌套字典转换为数据框?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43305643/