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.
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’ll explain how to convert a series into 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 into_dict()
allows customization of the output format, offering options likedict
,list
,series
,split
,records
, andindex
.
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()
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=)
Initialize Pandas Series
A Pandas Series is a core data structure in the Pandas library, representing one-dimensional data with labeled indices. It can hold various data types, such as strings, integers, floats, and other Python objects.
First, 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 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.
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 !!
Related Articles
- Pandas Series map() Function
- Pandas Series Drop duplicates() Function
- How to Make a Histogram in Pandas Series?
- Pandas Series.clip() Function
- Pandas Series iloc[] Function
- Convert DataFrame to Dictionary (Dict)
- Pandas Series sum() Function
- Pandas convert Series to string
- How to convert the Pandas Series to list?
- Pandas Series unique() Function with Examples
- How to Get the Length of a Series in Pandas?
- Pandas Series groupby() Function with Examples