Open In App

Python | Pandas Series.to_dict()

Last Updated : 11 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas

Series.to_dict()

function is used to convert the given Series object to {label -> value} dict or dict-like object.

Syntax: Series.to_dict(into=) Parameter : into : The collections.Mapping subclass to use as the return object. Returns : value_dict : collections.Mapping

Example #1:

Use

Series.to_dict()

function to convert the given series object to a dictionary.

# importing pandas as pd
import pandas as pd

# Creating the Series
sr = pd.Series(['New York', 'Chicago', 'Toronto', 'Lisbon', 'Rio', 'Moscow'])

# Create the Datetime Index
didx = pd.DatetimeIndex(start ='2014-08-01 10:00', freq ='W', 
                     periods = 6, tz = 'Europe/Berlin') 

# set the index
sr.index = didx

# Print the series
print(sr)

Output :

Now we will use

Series.to_dict()

function to convert the given series object to a dictionary.

# convert to dictionary
sr.to_dict()

Output :

As we can see in the output, the

Series.to_dict()

function has successfully converted the given series object to a dictionary.  

Example #2:

Use

Series.to_dict()

function to convert the given series object to a dictionary.

# importing pandas as pd
import pandas as pd

# Creating the Series
sr = pd.Series([19.5, 16.8, 22.78, 20.124, 18.1002])

# Print the series
print(sr)

Output :

Now we will use

Series.to_dict()

function to convert the given series object to a dictionary.

# convert to dictionary
sr.to_dict()

Output :

As we can see in the output, the

Series.to_dict()

function has successfully converted the given series object to a dictionary.

Python | Pandas Series.to_dict() – FAQs

What does to_dict() Do in Python?

The to_dict() method in pandas converts a DataFrame or Series into a dictionary. The method can take different forms depending on the desired output format:

  • For a DataFrame, to_dict() can convert the data into a dictionary of lists, a dictionary of dictionaries, etc., depending on the orient parameter.
  • For a Series, to_dict() converts the index-value pairs into key-value pairs in a dictionary.

How to Convert Series to DataFrame in Python Pandas

You can convert a pandas Series into a DataFrame using the to_frame() method. This is useful for when you need a DataFrame structure from a Series object.

# Convert Series to DataFrame
df = s.to_frame(name='Values')
print(df)

How to Convert a Series to a CSV File in Python

To write a Series directly to a CSV file, you can use the to_csv() method:

# Writing Series to CSV
s.to_csv('output.csv', header=True)

Here, header=True ensures that the header of the CSV (column name) is also written to the file.

How to Replicate a Series in Python

To replicate or repeat the elements of a Series, you can use the repeat() method:

# Replicate each element in the Series three times
replicated_series = s.repeat(3)
print(replicated_series)

How to Turn a Series into a Set in Python

Converting a Series into a set involves simply using the built-in set() function, which will remove any duplicate values and give you a set of unique elements from the Series:

# Convert Series to a set
series_set = set(s)
print(series_set)


Similar Reads

three90RightbarBannerImg