A dictionary is very useful to lookup values for certain keys. If you have a pandas series with a relevant index, you can create a python dict object with the “index: value” key-value pairs to quickly fetch values referred to by its index. In this tutorial, we will look at how to convert a pandas series to a python dictionary object.
How to convert a pandas series to a dict?
To get a dictionary from a series, you can use the pandas series to_dict()
function which returns a dictionary of “index: value” key-value pairs. The following is the syntax:
# using to_dict() d = s.to_dict()
Here, s is the pandas series you want to convert to a dictionary. The returned dictionary will have the series’ index as its keys and the series’ value as its value.
Examples
Let’s look at some examples of using the above method to create a dictionary from a series. First, we’ll create a sample pandas series which we will be using throughout this tutorial.
import pandas as pd # pandas series Wimbledon winners from 2015 to 2019 wimbledon_winners = pd.Series(index=[2015, 2016, 2017, 2018, 2019], data=['Novak Djokovic', 'Andy Murray', 'Roger Federer', 'Novak Djokovic', 'Novak Djokovic'], name='Name') # display the series print(wimbledon_winners)
Output:
2015 Novak Djokovic 2016 Andy Murray 2017 Roger Federer 2018 Novak Djokovic 2019 Novak Djokovic Name: Name, dtype: object
You can see the contents of the series object above. Let’s confirm the type of the object.
# check the type print(type(wimbledon_winners))
Output:
<class 'pandas.core.series.Series'>
We now have a pandas series containing the name of Wimbledon Winners from 2015 to 2019 with the year as its index.
Series to dict using to_dict()
You can use the pandas series to_dict()
function to create a dictionary object from a series. Let’s use it to create a dictionary with the year as its key and the winners’ name as its value from the series “wimbledon_winners” –
winners_dict = wimbledon_winners.to_dict() # check the type print(type(winners_dict)) # print the content print(winners_dict)
Output:
<class 'dict'> {2015: 'Novak Djokovic', 2016: 'Andy Murray', 2017: 'Roger Federer', 2018: 'Novak Djokovic', 2019: 'Novak Djokovic'}
You can see that here the pandas series to_dict()
function returns a dictionary of the “Year: Name” key-value pairs. In our case, we can use the above dictionary to quickly fetch results to questions like “Who was the Wimbledon winner in the year 2017?”
print(winners_dict[2017])
Output:
Roger Federer
We get the name corresponding to the key 2017 from the dictionary, “Roger Federer”.
For more on the pandas series to_dict() function, refer to its documentation.
With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.
Tutorials on pandas series –
- Convert Pandas Series to a DataFrame
- Convert Pandas Series to a List
- Convert Pandas Series to a NumPy Array
- Convert Pandas Series to a Dictionary
- Sort a Pandas Series
- Append Two Pandas Series
- Apply a Function to a Pandas Series
- Pandas – Shift column values up or down
- Plot a Histogram of Pandas Series Values
- Create a Pie Chart of Pandas Series Values
- Plot a Bar Chart of Pandas Series Values
- Create a Boxplot from Pandas Series Values
- Create a Density Plot from Pandas Series Values