python - Creating DataFrame with Hierarchical Columns -


what easiest way create dataframe hierarchical columns?

i creating dataframe dict of names -> series using:

df = pd.dataframe(data=serieses)

i use same columns names add additional level of hierarchy on columns. time being want additional level have same value columns, let's "estimates".

i trying following not seem work:

pd.dataframe(data=serieses,columns=pd.multiindex.from_tuples([(x, "estimates") x in serieses.keys()]))

all dataframe nans.

for example, looking roughly:

l1               estimates     l2  1  2  1  2  1  2  1  2 r1   1    2    3    4    5    6    7    8 r2   1.1  2    3    4    5    6    71   8.2 

where l1 , l2 labels multiindex

this appears work:

import pandas pd  data = {'a': [1,2,3,4], 'b': [10,20,30,40],'c': [100,200,300,400]}  df = pd.concat({"estimates": pd.dataframe(data)}, axis=1, names=["l1", "l2"])  l1  estimates          l2            b    c 0           1  10  100 1           2  20  200 2           3  30  300 3           4  40  400 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -