是否可以在 Pandas 中创建带有子标题的数据框? [英] Is it possible to create dataframe with sub-headers in Pandas?
问题描述
我有以下一段代码:
_tmp = {}_tmp['pre'] = {'A2': 10,'B2': 15,'C2':20}_tmp['差异'] = {'A1': 10,'B1': 15,'C1':20}_tmp['总和'] = {'A':100,'B':150,'C':200}
通过执行以下命令:
_dff = pd.DataFrame(data=_tmp.values(), index=_tmp.keys())_dff[['A', 'B', 'C', 'A1', 'B1', 'C1', 'A2', 'B2', 'C2']]
我得到了如下描述的结果:
但是我很想知道是否可以在 Pandas 中构建如下所示的结构:
您真正看到的是三维结构,这是数据框无法做到的.(它们只有 2D.)
这给您留下了几个选择:
(1) 多个数据帧(pre/diff/sum).
dfs = {k:pd.DataFrame(v.items()) for k,v in _tmp.items()}
(2) Pandas Panel
你可以像这样构建:
pnl = pd.Panel(dfs)
这是一种对多个数据帧进行分组的方法.
(3) 一个 3d numpy 矩阵:
<预><代码>>>>pnl.as_matrix()[[['A1' 10]['C1' 20]['B1' 15]][['C2' 20]['A2' 10]['B2' 15]][['A' 100]['C' 200]['B' 150]]]当然,您将不得不编写一个自定义打印函数来获得您所请求的确切输出,但这将是您表示数据的方式.
I have the following piece of code:
_tmp = {}
_tmp['pre'] = {
'A2': 10,
'B2': 15,
'C2': 20
}
_tmp['diff'] = {
'A1': 10,
'B1': 15,
'C1': 20
}
_tmp['sum'] = {
'A': 100,
'B': 150,
'C': 200
}
By performing the following commands:
_dff = pd.DataFrame(data=_tmp.values(), index=_tmp.keys())
_dff[['A', 'B', 'C', 'A1', 'B1', 'C1', 'A2', 'B2', 'C2']]
I got a result described below:
However I am interested in knowing whether it is possible to build a structure in pandas like the following:
What you're really looking at there is a three dimensional structure, which a dataframe cannot do. (They're 2D only.)
This leaves you with a couple options:
(1) Multiple dataframes (pre/diff/sum).
dfs = {k:pd.DataFrame(v.items()) for k,v in _tmp.items()}
(2) A pandas Panel
which you can build like:
pnl = pd.Panel(dfs)
Which is a way of grouping multiple dataframes.
(3) A 3d numpy matrix:
>>> pnl.as_matrix()
[[['A1' 10]
['C1' 20]
['B1' 15]]
[['C2' 20]
['A2' 10]
['B2' 15]]
[['A' 100]
['C' 200]
['B' 150]]]
You're going to have to write a custom print function to get your exact requested output of course, but these would be the ways you can represent your data.
这篇关于是否可以在 Pandas 中创建带有子标题的数据框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!