Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Dictionary to Dataframe with keys as index

Having this dictionary

{'ON':                   time     atm   rr25    bf25   rr10   bf10
 0  2022-03-29 23:00:00  0.0895 -0.008  0.0015 -0.014  0.004,
 '1W':                   time     atm  rr25   bf25   rr10    bf10
 0  2022-03-29 23:00:00  0.0785 -0.01  0.002 -0.017  0.0065,
 '2W':                   time     atm   rr25    bf25   rr10     bf10
 0  2022-03-29 23:00:00  0.0795 -0.011  0.0025 -0.019  0.00837,
 '1M':                   time    atm    rr25    bf25    rr10     bf10
 0  2022-03-29 23:00:00  0.077 -0.0115  0.0025 -0.0207  0.00925,
 '2M':                   time    atm    rr25    bf25    rr10     bf10
 0  2022-03-29 23:00:00  0.075 -0.0115  0.0025 -0.0207  0.00937,
 '3M':                   time      atm   rr25     bf25    rr10     bf10
 0  2022-03-29 23:00:00  0.07325 -0.012  0.00255 -0.0222  0.00969,
 '6M':                   time      atm    rr25     bf25     rr10    bf10
 0  2022-03-29 23:00:00  0.07175 -0.0125  0.00265 -0.02375  0.0102,
 '9M':                   time      atm    rr25     bf25     rr10    bf10
 0  2022-03-29 23:00:00  0.07125 -0.0125  0.00295 -0.02375  0.0115,
 '1Y':                   time    atm    rr25   bf25     rr10    bf10
 0  2022-03-29 23:00:00  0.071 -0.0125  0.003 -0.02381  0.0117,
 '2Y':                   time    atm    rr25    bf25     rr10     bf10
 0  2022-03-29 23:00:00  0.072 -0.0125  0.0032 -0.02375  0.01248}

I would like to convert it to this dataFrame

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I tried different approaches by using pd.DataFrame.from_dict but didn’t help

>Solution :

Use pd.concat:

df = pd.concat(data).droplevel(1)

Output:

>>> df
                   time     atm   rr25    bf25   rr10    bf10
ON  2022-03-29 23:00:00  0.0895 -0.008  0.0015 -0.014  0.0040
1W  2022-03-29 23:00:00  0.0785 -0.010  0.0020 -0.017  0.0065

Data

>>> data
{'ON':                   time     atm   rr25    bf25   rr10   bf10
 0  2022-03-29 23:00:00  0.0895 -0.008  0.0015 -0.014  0.004,
 '1W':                   time     atm  rr25   bf25   rr10    bf10
 0  2022-03-29 23:00:00  0.0785 -0.01  0.002 -0.017  0.0065}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading