• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:7 mins read
You are currently viewing Convert Series to Dictionary(Dict) in Pandas

Pandas Series.to_dict() function is used to convert Series to Dictionary (dict) objects. You can use this method if you have a Series with a relevant index and want to convert it to a Python dictionary (dict) object by converting indices of series as keys and the values of series as values.

Series.to_dict() takes param orient which is used the specify the output format. It takes values 'dict''list''series''split''records', and 'index'. In this tutorial, I will explain how to convert a series to a Python dictionary object with examples.

1. Quick Examples of Convert Series to Dict

If you are in a hurry below are some quick examples of how to convert the pandas series to Dictionary.


# Below are the quick examples

# Example 1 : Create Pandas series
courses = pd.Series(['Java', 'Spark', 'PySpark','Pandas','NumPy', 'Python'])

# Example 2 : Customize the index series
courses = pd.Series(['Java', 'Spark', 'PySpark','Pandas','NumPy', 'Python'],
                    index = ['Course1', 'Course2', 'Course3', 'Course4', 'Course5','Course6'])

# Example 3 : Convert Series to dictionary
dict = courses.to_dict() 

2. Syntax of Pandas Series.to_dict()

Following is the syntax of the Series.to_dict().


# Syntax of series.to_dict().
series.to_dict(orient='dict', into=)

3. Create Pandas Series

Pandas Series is a one-dimensional, Index-labeled data structure available in the Pandas library. It can store all the datatypes such as strings, integers, floats, and other Python objects. We can access each element in the Series with the help of corresponding default labels.

Now, let’s create a series and change it to a Python dictionary. Note that our series contains indices and values.


import pandas as pd
# Customize the index series
courses = pd.Series(['Java', 'Spark', 'PySpark','Pandas','NumPy', 'Python'],
                    index = ['Course1', 'Course2', 'Course3', 'Course4', 'Course5','Course6'])
print("Create Pandas Series:\n", courses)

Yields below output.

convert series dict

4. Convert Pandas Series to Dictionary

You can use the pandas Series.to_dict() function to convert a Series to a dictionary (dict). If you want to convert DataFrame to Dictionary you can use DataFrame.to.dict() function.

Let’s pass the Series object which we want to convert to a dictionary into this function, it will return the dictionary, where the keys are indices and values are values of the given series.


# Convert Series to dictionary
dict = courses.to_dict()
print("After converting a Series to dictionary:\n", dict)
print("Type of the object:\n", type(dict))

Yields below output.

convert series dict

5. Conclusion

In this article, I have explained pandas.Series.to_dict() method and using this method we can convert Pandas Series to Python dictionary objects by converting indices as keys and the corresponding values of indices as values.

Happy Learning !!

References

Vijetha

Vijetha is an experienced technical writer with a strong command of various programming languages. She has had the opportunity to work extensively with a diverse range of technologies, including Python, Pandas, NumPy, and R. Throughout her career, Vijetha has consistently exhibited a remarkable ability to comprehend intricate technical details and adeptly translate them into accessible and understandable materials. Follow me at Linkedin.