Create a dictionary from pandas columns
We are able to convert one or more pandas Series to a dictionary using the to_dict Series and DataFrame methods:
#Series
my_series.to_dict()
#Multiple DataFrame columns
my_df[[col1, col2]].to_dict()
Step #1 Create DataFrame
We’ll start by importing the pandas Data Analysis library and creating a DataFrame.
import pandas pd
raw_dict = {
'channel' : ['B2C', 'B2B', 'Online'],
'sales' : [250, 345, 432]
}
perf_df = pd.DataFrame (raw_dict)
perf_df.head()
Here’s our dataset:
channel | sales | |
---|---|---|
0 | B2C | 250 |
1 | B2B | 345 |
2 | Online | 432 |
Step #2: Using to_dict to convert a single column to a dictionary object
We can easily pull the contents of a Series into a dictionary using the to_dict() Series method.
perf_df.channel.to_dict()
This will return a dictionary made of key / value pairs consisting of the Series index and elements:
{0: 'B2C', 1: 'B2B', 2: 'Online'}
Step #3: Turn multiple column into dictionary
Next case is to convert the entire DataFrame into a Python dictionary object.
sales_dict = perf_df.to_dict()
print(sales_dict)
Or converting specific selected multiple columns, by passing a list of relevant columns to the to_dict() function:
sales_dict = perf_df [['channel', 'sales']].to_dict()
print(sales_dict)
Both will return the following object:
{'channel': {0: 'B2C', 1: 'B2B', 2: 'Online'}, 'sales': {0: 250, 1: 345, 2: 432}}
Step #4: Zip two columns and make a dictionary
Another way to turn our columns to a dictionary object is to zip the two Series, then convert them into a list and last build the dictionary.
sales_lst = list(zip(perf_df.channel,perf_df.sales ))
sales_dict = dict(sales_lst)
This will result in:
{'sales': {'B2C': 250, 'B2B': 345, 'Online': 432}}
Note that the dictionary doesn’t include the DataFrame index column. A similar way to accomplish the same result would have been:
sales_dict = perf_df.set_index('channel').to_dict()