python - pandas: Problems with merging multiple data frames to get desired MultiIndex -


i have defined 3 simple data frames follows:

dates = [dt.datetime(2015,1,1),dt.datetime(2015,1,2)] column_names = ['a','b'] data_open = pd.dataframe([[1,2],[3,4]], index=dates, columns=column_names) data_close = pd.dataframe([[5,6],[7,8]], index=dates, columns=column_names) data_volume = pd.dataframe([[9,10],[11,12]], index=dates, columns=column_names) 

when printed follows:

print data_open              b 2015-01-01  1  2 2015-01-02  3  4  print data_close              b 2015-01-01  5  6 2015-01-02  7  8  print data_volume                b 2015-01-01   9  10 2015-01-02  11  12 

what looking way merge 3 of them , single dataframe multiindex this:

desired data frame structure

i new pandas , looked in documentation , not find looking for.

you can use concat specify keys , swaplevel , sort_index:

df = pd.concat([data_open, data_close, data_volume], keys=['open', 'close', 'volume']) df = df.swaplevel(0,1).sort_index()  print df                        b 2015-01-01 close    5   6            open     1   2            volume   9  10 2015-01-02 close    7   8            open     3   4            volume  11  12 

and if want sort column a:

df = df.groupby(level=0).apply(lambda x: x.sort_values('a')) df.index = df.index.droplevel(0) print df                        b 2015-01-01 open     1   2            close    5   6            volume   9  10 2015-01-02 open     3   4            close    7   8            volume  11  12 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -