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:
office | interviews | hired | |
---|---|---|---|
0 | Los Angeles | 104 | 15 |
1 | New York | 148 | 24 |
2 | Osaka | 166 | 23 |
3 | Tokyo | 213 | 25 |
4 | Buenos Aires | 149 | 27 |
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 Angeles | Tokyo | New York | |
---|---|---|---|
interviews | 110 | 130 | 149 |
hired | 14 | 15 | 16 |
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