pandas series to python dictionary transition

Convert Pandas Series to a Dictionary

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.

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.

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.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

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 –

Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top