Home > Net >  Constructing DataFrame from dict with rows instead of columns
Constructing DataFrame from dict with rows instead of columns

Time:11-17

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:

    (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

CodePudding user response:

You can use transpose:

data_results_1y=data_results_1y.T
  • Related