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
                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.
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.
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
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With