Wei-Ting Liao
Wei-Ting Liao

Reputation: 115

Pandas data frame to dictionary with two keys from index and columns

I am working on converting data frame to dictionary with two keys, which one is from index and another is from columns. I found a way with "for loops". I am curious that is there any more efficient way for this?

HarvesterData_df = pd.read_csv('HarvesterData.csv').set_index('Feature')
HarvesterData_df.index.name = None

The data frame looks like this.

                          265 HP Combine  305 HP Combine  385 HP Combine
  PurchasePrice              291550.00       342125.00       430950.00   
  AnnualInterest              42671.91        50074.18        63074.80   
  Throughput                     33.19           44.26           66.39   
  Speed                           5.50            5.50            5.50   
  Width                           4.57            6.10            9.14   
  Efficiency                      0.90            0.90            0.90   
  Horsepower                    265.00          305.00          385.00   
  FuelConsumption                11.61           13.36           16.86   
  LaborCost                      14.50           14.50           14.50   
  CombinePrice               293000.00       337000.00       407000.00   
  CornHeadPrice               50000.00        65500.00       100000.00   
  RowNumber                       6.00            8.00           12.00   
  TheorticalThroughput           27.66           36.88           55.32   

And I want to convert it to a dictionary has keys based on index and columns. I try:

HarvesterData = {(r,c): HarvesterData_df.loc[r][c] for r in HarvesterData_df.index.tolist() for c in HarvesterData_df.columns.tolist()}

The output is perfect and exactly what I want. However, can I do it without loops in Pandas?

{('AnnualInterest', '265 HP Combine'): 42671.910000000003,
 ('AnnualInterest', '305 HP Combine'): 50074.18,
 ('AnnualInterest', '385 HP Combine'): 63074.800000000003,
 ('AnnualInterest', '440 HP Combine'): 72778.619999999995,
 ('CombinePrice', '265 HP Combine'): 293000.0,
 ('CombinePrice', '305 HP Combine'): 337000.0,
 ('CombinePrice', '385 HP Combine'): 407000.0,
 ('CombinePrice', '440 HP Combine'): 455000.0,
 ('CornHeadPrice', '265 HP Combine'): 50000.0,
 ('CornHeadPrice', '305 HP Combine'): 65500.0,
 ('CornHeadPrice', '385 HP Combine'): 100000.0,
 ('CornHeadPrice', '440 HP Combine'): 130000.0,
 ('Efficiency', '265 HP Combine'): 0.90000000000000002,
 ('Efficiency', '305 HP Combine'): 0.90000000000000002,
 ('Efficiency', '385 HP Combine'): 0.90000000000000002,
 ('Efficiency', '440 HP Combine'): 0.90000000000000002, ...

Upvotes: 2

Views: 1515

Answers (1)

Ami Tavory
Ami Tavory

Reputation: 76297

You simply need to stack and then call to_dict:

import pandas as pd
df = pd.DataFrame({'a': [1, 2], 'b': [3, 4]})
>> df
    a   b
0   1   3
1   2   4

>> df.stack().to_dict()
{(0, 'a'): 1, (0, 'b'): 3, (1, 'a'): 2, (1, 'b'): 4}

Upvotes: 5

Related Questions