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

In Pandas, you can convert a Series to a dictionary using the to_dict() method. This method creates a dictionary where the index labels of the Series become the keys, and the corresponding values become the values in the dictionary.

Advertisements

The to_dict() method in Pandas allows you to convert a Series into a Python dictionary object. This method offers flexibility through its orient parameter, which specifies the output format. The orient parameter accepts values such as dict, list, series, split, records, and index. In this tutorial, I will explain convert a series to a Python dictionary object.

Key Points –

  • Pandas provides the to_dict() method to convert a Series to a Python dictionary.
  • The resulting dictionary has the index labels of the Series as keys and the corresponding values as values.
  • This conversion is useful for scenarios where dictionary-like operations or interfaces are required.
  • The orient parameter in to_dict() allows customization of the output format, offering options like ‘dict’, ‘list’, ‘series’, ‘split’, ‘records’, and ‘index’.

Quick Examples of Convert Series to Dict

Below are quick examples of converting the pandas series to Dictionary.


# Quick examples of convert series to dict

# 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() 

Pandas Series.to_dict() Introduction

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


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

Create Pandas Series

A Pandas Series is a fundamental data structure within the Pandas library. It represents one-dimensional, index-labeled data. This data structure can accommodate various data types, including strings, integers, floats, and other Python objects.

To run some examples of converting series to dictionary(dict) in pandas, 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

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

Conclusion

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

Happy Learning !!

References