Skip to content Skip to sidebar Skip to footer

Is It Possible To Create Dataframe With Sub-headers In Pandas?

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'] = {

Solution 1:

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.

Post a Comment for "Is It Possible To Create Dataframe With Sub-headers In Pandas?"