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

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • 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.  Check if Variable is Integer in Python
  • 2.  Using Selenium to Close Browser in Python
  • 3.  pandas interpolate() – Fill NaN Values with Interpolation in DataFrame
  • 4.  How to Return Nothing in Python from Function
  • 5.  Using Python to Convert Integer to String with Leading Zeros
  • 6.  Get Days in Month from Date in pandas DataFrame
  • 7.  pandas percentile – Calculate Percentiles of Series or Columns in DataFrame
  • 8.  Using Python Selenium Webdriver to Refresh Page
  • 9.  Convert String into Tuple in Python
  • 10.  Using Python to Read File Word by Word

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