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

Constructing DataFrame from dict with rows instead of columns

I have a dict with the following setup:

{('CMS', 'LNT'): 0.8500276624334894,
 ('LNT', 'CMS'): 0.8500276624334894,
 ('LOW', 'HD'): 0.8502400376842035,
 ('HD', 'LOW'): 0.8502400376842036,
 ('SWKS', 'QRVO'): 0.8507993847326996,
 ('QRVO', 'SWKS'): 0.8507993847326996,
 ('WFC', 'BAC'): 0.8510581675586776,
.....

Now I want to turn this dict into a data frame. I am using the following code, where abc is the name of the dict:

data_results_1y = pd.DataFrame.from_records(abc, index=[0])
data_results_1y

The output of the code is:

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

    (AAL, DAL)  (AAL, UAL)  (AEE, CMS)  (AEE, LNT)  (AEE, WEC)  (AEP, XEL)
0   0.873762    0.898774    0.859258    0.853194    0.8679  0.851787

However I want the Dataframe to have multiple rows instead of multiple columns.
So the output should look like this:

(AAL, DAL)  0.873762
(AAL, UAL)  0.898774
(AEE, CMS)  0.859258
(AEE, LNT)  0.853194
(AEE, WEC)  0.8679
(AEP, XEL)  0.851787

Where the key combination of the dict is the index.

How can I achieve this ?

I have already tried to set the index in the Dataframe construction as abc.key, abc.values which didn’t work

>Solution :

You can use transpose:

data_results_1y=data_results_1y.T
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