• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Convert pandas Series to Dictionary in Python

Convert pandas Series to Dictionary in Python

October 14, 2022 Leave a Comment

To convert a pandas Series to a dictionary in Python, the easiest way by using to_dict() on a pandas Series.

import pandas as pd

df = pd.DataFrame({"animal_type":["dog","cat","dog","cat","dog","dog","cat","cat","dog"], 
                   "gender":["F","F","F","F","M","M","M","F","M"], 
                   "age":[1,2,3,4,5,6,7,8,9], 
                   "weight":[10,20,15,20,25,10,15,30,40]})

animal_types = df["animal_type"].to_dict()

print(animal_types)

#Output:
{0: 'dog', 1: 'cat', 2: 'dog', 3: 'cat', 4: 'dog', 5: 'dog', 6: 'cat', 7: 'cat', 8: 'dog'}

If you don’t want this format, you can use to_dict() on the entire DataFrame which gives you have a few different orientation options: ‘dict’, ‘list’, ‘series’, ‘split’, ‘records’, ‘index’. Then you can access the values you want after the conversion.

import pandas as pd

df = pd.DataFrame({"animal_type":["dog","cat","dog","cat","dog","dog","cat","cat","dog"], 
                   "gender":["F","F","F","F","M","M","M","F","M"], 
                   "age":[1,2,3,4,5,6,7,8,9], 
                   "weight":[10,20,15,20,25,10,15,30,40]})

df_as_dict = df.to_dict(orient="list")

print(df_as_dict)
print(df_as_dict["animal_type"])

#Output:
{'animal_type': ['dog', 'cat', 'dog', 'cat', 'dog', 'dog', 'cat', 'cat', 'dog'], 'gender': ['F', 'F', 'F', 'F', 'M', 'M', 'M', 'F', 'M'], 'age': [1, 2, 3, 4, 5, 6, 7, 8, 9], 'weight': [10, 20, 15, 20, 25, 10, 15, 30, 40]}
{'animal_type': ['dog', 'cat', 'dog', 'cat', 'dog', 'dog', 'cat', 'cat', 'dog']}

You can also use dict() function to convert a pandas Series to a dictionary.

import pandas as pd

df = pd.DataFrame({"animal_type":["dog","cat","dog","cat","dog","dog","cat","cat","dog"], 
                   "gender":["F","F","F","F","M","M","M","F","M"], 
                   "age":[1,2,3,4,5,6,7,8,9], 
                   "weight":[10,20,15,20,25,10,15,30,40]})

animal_types = dict(df["animal_type"])

print(animal_types)

#Output:
{0: 'dog', 1: 'cat', 2: 'dog', 3: 'cat', 4: 'dog', 5: 'dog', 6: 'cat', 7: 'cat', 8: 'dog'}

When working with collections of data, the ability to be able to easily access certain pieces of information is valuable.

One such situation is if you want to get the values of a pandas Series and create a dictionary in Python.

There are a few ways you can create a dictionary from the values of a pandas Series.

Having the values of a Series in a list can be useful if you want to loop over the values and perform an action.

To convert a pandas Series to a dictionary in Python, the easiest way by using to_dict() on a pandas Series.

Below shows a simple example of how you can create a dictionary from the values of a pandas Series with to_dict() in Python.

import pandas as pd

df = pd.DataFrame({"animal_type":["dog","cat","dog","cat","dog","dog","cat","cat","dog"], 
                   "gender":["F","F","F","F","M","M","M","F","M"], 
                   "age":[1,2,3,4,5,6,7,8,9], 
                   "weight":[10,20,15,20,25,10,15,30,40]})

animal_types = df["animal_type"].to_dict()

print(animal_types)

#Output:
{0: 'dog', 1: 'cat', 2: 'dog', 3: 'cat', 4: 'dog', 5: 'dog', 6: 'cat', 7: 'cat', 8: 'dog'}

If you don’t want this format, you can use to_dict() on the entire DataFrame which gives you have a few different orientation options: ‘dict’, ‘list’, ‘series’, ‘split’, ‘records’, ‘index’. Then you can access the values you want after the conversion.

Below is an example of orienting the converted DataFrame as a dictionary so that the values of each column are in a list with ‘list’ and then accessing the “animal_type” to get the values of “animal_type”.

import pandas as pd

df = pd.DataFrame({"animal_type":["dog","cat","dog","cat","dog","dog","cat","cat","dog"], 
                   "gender":["F","F","F","F","M","M","M","F","M"], 
                   "age":[1,2,3,4,5,6,7,8,9], 
                   "weight":[10,20,15,20,25,10,15,30,40]})

df_as_dict = df.to_dict(orient="list")

print(df_as_dict)
print(df_as_dict["animal_type"])

#Output:
{'animal_type': ['dog', 'cat', 'dog', 'cat', 'dog', 'dog', 'cat', 'cat', 'dog'], 'gender': ['F', 'F', 'F', 'F', 'M', 'M', 'M', 'F', 'M'], 'age': [1, 2, 3, 4, 5, 6, 7, 8, 9], 'weight': [10, 20, 15, 20, 25, 10, 15, 30, 40]}
{'animal_type': ['dog', 'cat', 'dog', 'cat', 'dog', 'dog', 'cat', 'cat', 'dog']}

Using dict() to Convert pandas Series to Dictionary in Python

Another way you can convert the pandas Series values to a dictionary is with the Python dict() function.

dict() tries to convert a Python object to a dictionary. The dictionary representation of a pandas Series is each key/value pair is the index and value of the Series.

Below shows another example of how you can get the Series values of a pandas Series as a dictionary in Python.

import pandas as pd

df = pd.DataFrame({"animal_type":["dog","cat","dog","cat","dog","dog","cat","cat","dog"], 
                   "gender":["F","F","F","F","M","M","M","F","M"], 
                   "age":[1,2,3,4,5,6,7,8,9], 
                   "weight":[10,20,15,20,25,10,15,30,40]})

animal_types = dict(df["animal_type"])

print(animal_types)

#Output:
{0: 'dog', 1: 'cat', 2: 'dog', 3: 'cat', 4: 'dog', 5: 'dog', 6: 'cat', 7: 'cat', 8: 'dog'}

Hopefully this article has been useful for you to be able to learn how to convert pandas Series to a dictionary in Python.

Other Articles You'll Also Like:

  • 1.  How to Group By Columns and Find Variance in pandas
  • 2.  Calculate Sum of Dictionary Values in Python
  • 3.  islower Python – Check if All Letters in String Are Lowercase
  • 4.  pandas cumprod – Find Cumulative Product of Series or DataFrame
  • 5.  Python turtle dot() – Draw Dot on Turtle Screen
  • 6.  Using Python to Reverse Tuple
  • 7.  Run Something Every 5 Seconds in Python
  • 8.  Difference Between read(), readline() and readlines() in Python
  • 9.  Pascal’s Triangle in Python
  • 10.  Get Day Name from Datetime in pandas DataFrame

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy