Python 如何将嵌套字典转换为数据帧?
我有一本嵌套字典。这是纳斯达克的数据。像这样:Python 如何将嵌套字典转换为数据帧?,python,pandas,dictionary,dataframe,Python,Pandas,Dictionary,Dataframe,我有一本嵌套字典。这是纳斯达克的数据。像这样: {'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
{'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\u frame().reset\u 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
也许pandas.concat就是您要寻找的:
In [8]: data = dict(A=pd.DataFrame([[1,2], [3,4]], columns=['X', 'Y']),
B=pd.DataFrame([[1,2], [3,4]], columns=['X', 'Y']),)
In [9]: data
Out[9]:
{'A': X Y
0 1 2
1 3 4,
'B': X Y
0 1 2
1 3 4}
In [10]: pd.concat(data)
Out[10]:
X Y
A 0 1 2
1 3 4
B 0 1 2
1 3 4
更新: 假设
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
旧答案-它假设
日期
是一个常规列(不是索引)
试试这个:
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
是您的嵌套字典
:
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运行太长,无法输出结果。我使用这个:
df=pd.concat(pd.Panel(nas)[k]表示nas.keys()中的k
,它将永远运行。我按照您所说的做了,但它返回了一个错误:KeyError:'Level major not found'
,我几乎不理解您的代码的意思,swapaxes(0,2)
,而且我似乎没有major
的定义,您也可以使用reset\u索引(level=0,drop=True)
。但这很奇怪,因为在执行reset\u index()
之后,我希望出现major
列……太棒了!它的所有输出都很小,但是日期栏不见了,就像是小开盘高收盘低成交量调整收盘A 41.900002 42.349998 41.720001 41.810001 1449300.0 41.357005 A 37.3699937.950001 37.000000 37.689992666200.0 37.281641 A 37.40000238.0299937.400002 37.610001831200.0 37.202510A 40.240002 40.9900040.0499940.730000 2103600.0 40.288705,您能解释一下吗???@PanKevin,您能输出打印(nas['CLSN'].head())
?您的Date
列是否可能是字典中原始DFs的索引?我已经发布了它,我完全同意Date
列是DF的索引。在我发布问题之前,我对输出做了一些编辑,如果这让你感到困惑,我真的很抱歉。。。