How to convert a dictionary with multiple values to pandas DataFrames?

Step 1: Define Python dictionary from lists

import pandas as pd

office = ['Los Angeles', 'New York', 'Osaka', 'Tokyo', 'Buenos Aires', 'Bangalore']
interviews= [104, 148, 166, 213, 149, 187]
hired = [15, 24, 23, 25, 27, 30]
hiring = dict(office = office, interviews = interviews, hired = hired)
hiring

Here’s the dictionary that we have created:

{'office': ['Los Angeles',
  'New York',
  'Osaka',
  'Tokyo',
  'Buenos Aires',
  'Bangalore'],
 'interviews': [104, 148, 166, 213, 149, 187],
 'hired': [15, 24, 23, 25, 27, 30]}

Step 2: Convert multiple values dictionary to DataFrame

We can now create a pandas DataFrame from the multiple value dictionary:

test_df = pd.DataFrame(data=hiring)

Here’s our DataFrame:

officeinterviewshired
0Los Angeles10415
1New York14824
2Osaka16623
3Tokyo21325
4Buenos Aires14927

Step 3: Convert a nested dictionary to DataFrame

You might as well get a more complex nested dictionary, which is basically a dict object containing other dictionaries. Here is a simple example:

nested_hiring = {'Los Angeles': {'interviews' : 110, 'hired': 14},
 'Tokyo' : {'interviews' : 130, 'hired': 15},
 'New York' : {'interviews' : 149, 'hired': 16}
}

We can turn the nested dictionary to a DataFrame by passing the dict object to the constructor:

nested_df = pd.DataFrame(data=nested_hiring)
nested_df

Here’s our data – note that each dictionary was converted to a column:

Los AngelesTokyoNew York
interviews110130149
hired141516

Step 4: Plotting your dictionary

We can optionally create a chart from our dictionary using pandas.

test_df.plot(kind='bar', x='office', title = 'Interviews vs Hired', cmap= 'viridis');

Here’s our bar plot:

Troubleshooting

  • When using the dict() Python function, make sure to spell the names of your input lists correctly. For example – the following code will render an error:
# we purposely spelled the hired column incorrectly
hiring = dict(office = office, interviews = interviews, hired = hire)

A NameError exception will be thrown:

NameError: name 'hire' is not defined