Pandas DataFrame.to_dict() Function

Minahil Noor Jan 30, 2023
  1. Syntax of pandas.DataFrame.to_dict():
  2. Example Codes: DataFrame.to_dict() Method to Convert the DataFrame Into Dictionary of Dictionaries
  3. Example Codes: DataFrame.to_dict() Method to Convert the DataFrame Into Dictionary of Series
Pandas DataFrame.to_dict() Function

Python Pandas DataFrame.to_dict() function converts the given DataFrame to a dictionary.

Syntax of pandas.DataFrame.to_dict():

DataFrame.to_dict(orient='dict',
                  into= < class 'dict' >)

Parameters

orient This parameter determines the type of the dictionary. For example, it can be a dictionary of series or list. It has six options. These are dict, list, Series, split, records, and index.
into It is a class parameter. We can pass an actual class or its instance as a parameter.

Return

It returns the dictionary that represents the passed Dataframe.

Example Codes: DataFrame.to_dict() Method to Convert the DataFrame Into Dictionary of Dictionaries

To convert a DataFrame into a dictionary of dictionaries, we will pass no parameters.

import pandas as pd

dataframe=pd.DataFrame({'Attendance': {0: 60, 1: 100, 2: 80,3: 78,4: 95},
                        'Name': {0: 'Olivia', 1: 'John', 2: 'Laura',3: 'Ben',4: 'Kevin'},
                        'Obtained Marks': {0: 90, 1: 75, 2: 82, 3: 64, 4: 45}})
print("The Original Data frame is: \n")
print(dataframe)

dataframe1 = dataframe.to_dict()
print("The Dictionary of Dictionaries is: \n")
print(dataframe1)

Output:

The Original Data frame is: 

   Attendance    Name  Obtained Marks
0          60  Olivia              90
1         100    John              75
2          80   Laura              82
3          78     Ben              64
4          95   Kevin              45
The Dictionary of Dictionaries is: 

{'Attendance': {0: 60, 1: 100, 2: 80, 3: 78, 4: 95}, 'Obtained Marks': {0: 90, 1: 75, 2: 82, 3: 64, 4: 45}, 'Name': {0: 'Olivia', 1: 'John', 2: 'Laura', 3: 'Ben', 4: 'Kevin'}}

The function has returned the dictionary of dictionaries.

Example Codes: DataFrame.to_dict() Method to Convert the DataFrame Into Dictionary of Series

To convert a DataFrame into a dictionary of series, we will pass Series as the orient parameter.

import pandas as pd

dataframe=pd.DataFrame({'Attendance': {0: 60, 1: 100, 2: 80,3: 78,4: 95},
                        'Name': {0: 'Olivia', 1: 'John', 2: 'Laura',3: 'Ben',4: 'Kevin'},
                        'Obtained Marks': {0: 90, 1: 75, 2: 82, 3: 64, 4: 45}})
print("The Original Data frame is: \n")
print(dataframe)

dataframe1 = dataframe.to_dict('series')
print("The Dictionary of Series is: \n")
print(dataframe1)

Output:

The Original Data frame is: 

   Attendance    Name  Obtained Marks
0          60  Olivia              90
1         100    John              75
2          80   Laura              82
3          78     Ben              64
4          95   Kevin              45
The Dictionary of Series is: 

{'Attendance': 0     60
1    100
2     80
3     78
4     95
Name: Attendance, dtype: int64, 'Obtained Marks': 0    90
1    75
2    82
3    64
4    45
Name: Obtained Marks, dtype: int64, 'Name': 0    Olivia
1      John
2     Laura
3       Ben
4     Kevin
Name: Name, dtype: object}

The function has returned the dictionary of series.

Related Article - Pandas DataFrame