Convert Series to Dictionary(Dict) in Pandas

Spread the love

Pandas Series.to_dict() function is used to convert Series to Dictionary (dict) object. 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 Python 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 python dictionary. Note that our series contains index 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(courses)

Yields below output.


# Output
Course1       Java
Course2      Spark
Course3    PySpark
Course4     Pandas
Course5      NumPy
Course6     Python
dtype: object

5. Convert Pandas Series to Dictionary

Use pandas Series.to_dict() function to convert Series to a dictionary (dict). If you want convert DataFrame to Dictionary 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(dict)
print(type(dict))

Yields below output.


# Output
{'Course1': 'Java', 'Course2': 'Spark', 'Course3': 'PySpark', 'Course4': 'Pandas', 'Course5': 'NumPy', 'Course6': 'Python'}
<class 'dict'>

6. Conclusion

In this article, I have explained pandas.Series.to_dict() method which convert pandas Series to Dictionary (dict) object. Use this method to convert Series to python dictionary objects by converting indices as keys and the corresponding values of indices as values.

Happy Learning !!

References

Leave a Reply

You are currently viewing Convert Series to Dictionary(Dict) in Pandas