Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dictionary to multi-index pandas dataframe

Suppose I have dictionary looking like,

{('20170330', 'A'): {'earn': '16.02', 'lstdt': '2014/06/16', 'gap': '0.21','ocha': '5.44', 'nav': '77'},
('20170331', 'A'): {'earn': '25.68', 'lstdt': '2015/07/29','gap': '-1.41','ocha': '10.24', 'nav': '106'},
('20170331', 'B'): {'earn': '-', 'lstdt': '2016/09/12', 'gap':'-0.08', 'ocha': '0.79','nav': '145'}}

How could I make this to multi-index dataframe which resembles panel data?

Estimated outcome being,

                     earn         lstdt     gap    ocha    nav
     date    name   
 20170330      A   16.02    2014/06/16    0.21    5.44     77
 20170331      A   25.68    2015/07/29   -1.41   10.24    106
               B   -        2016/09/12   -0.08    0.79    145
like image 432
Hannah Lee Avatar asked May 09 '18 03:05

Hannah Lee


People also ask

How do you convert a nested dictionary to a DataFrame in Python?

Practical Data Science using PythonWe first take the list of nested dictionary and extract the rows of data from it. Then we create another for loop to append the rows into the new list which was originally created empty. Finally we apply the DataFrames function in the pandas library to create the Data Frame.

Can you put a dictionary in a pandas DataFrame?

A pandas DataFrame can be converted into a Python dictionary using the DataFrame instance method to_dict(). The output can be specified of various orientations using the parameter orient. In dictionary orientation, for each column of the DataFrame the column value is listed against the row label in a dictionary.


1 Answers

You can use from_dict(d, orient="index")

d = {...}
pd.DataFrame.from_dict(d, orient="index").rename_axis(["date", "name"])

the result:

                earn       lstdt    gap   ocha  nav
date     name                                      
20170330 A     16.02  2014/06/16   0.21   5.44   77
20170331 A     25.68  2015/07/29  -1.41  10.24  106
         B         -  2016/09/12  -0.08   0.79  145
like image 66
HYRY Avatar answered Sep 25 '22 17:09

HYRY