JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

Pandas DataFrame.to_dict() Function

Author:JIYIK Last Updated:2025/05/01 Views:

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


pandas.DataFrame.to_dict()Syntax

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

parameter

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

return

It returns a dictionary representing the passed Dataframe.


Example Code: DataFrame.to_dict()Method to Convert DataFrame to Dictionary of Dictionaries

To convert the DataFrame to a dictionary of dictionaries, we will not pass any arguments.

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'}}

This function returns a dictionary of dictionaries.


Example Code: How to DataFrame.to_dict()Convert DataFrame to SeriesDictionary

To convert a DataFrame to Seriesa dictionary of , we pass seriesas orientthe argument.

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 returns Seriesa dictionary.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Pandas DataFrame.astype() Function

Publish Date:2025/05/01 Views:160 Category:Python

Python Pandas DataFrame.astype() function changes the data type of an object to the specified data type. pandas.DataFrame.astype() grammar DataFrame . astype(dtype, copy = True , errors = "raise" ) parameter dtype The data type we want to a

Pandas DataFrame.reset_index() Function

Publish Date:2025/05/01 Views:140 Category:Python

Python Pandas DataFrame.reset_index() function resets the index of the given DataFrame. It replaces the old index with the default index. If the given DataFrame has a MultiIndex, then this method removes all the levels. pandas.DataFrame.rep

Pandas DataFrame.resample() Function

Publish Date:2025/05/01 Views:78 Category:Python

Python Pandas DataFrame.resample() function resamples time series data. pandas.DataFrame.resample() Syntax DataFrame . resample( rule, axis = 0 , closed = None , label = None , convention = "start" , kind = None , loffset = None , base = No

Pandas DataFrame.insert() Function

Publish Date:2025/05/01 Views:116 Category:Python

Python Pandas DataFrame.insert() function inserts a column at the specified position into the DataFrame. pandas.DataFrame.insert() Syntax DataFrame . insert(loc, column, value, allow_duplicates = False ) parameter loc It is an integer param

Pandas DataFrame.idxmax() Function

Publish Date:2025/05/01 Views:79 Category:Python

Python Pandas DataFrame.idxmax() function returns the index of the maximum value in a row or column. pandas.DataFrame.idxmax() Syntax DataFrame . idxmax(axis = 0 , skipna = True ) parameter axis It is a parameter of integer or string type.

Pandas DataFrame sort_index() Function

Publish Date:2025/05/01 Views:183 Category:Python

This tutorial explains how to use pandas.DataFrame.sort_index() the sort method to sort a Pandas DataFrame based on its index. We will use the DataFrame shown in the above example to explain how to sort a Pandas DataFrame based on the index

Pandas cut function

Publish Date:2025/05/01 Views:165 Category:Python

pandas.cut() The function can distribute the given data into a range, which can also be called bins . We will use the following DataFrame in this article. import pandas as pd df = pd . DataFrame( { "Name" : [ "Anish" , "Birat" , "Chirag" ,

Appending to an Empty DataFrame in Pandas

Publish Date:2025/05/01 Views:54 Category:Python

As we learned earlier, Pandas in Python is an open source module that we can use for data analysis and making machine learning models. It is Numpy used along with another package called as they go hand in hand to support multidimensional ar

Pandas DataFrame DataFrame.query() function

Publish Date:2025/04/30 Views:108 Category:Python

The pandas.DataFrame.query() method filters the rows of the caller DataFrame using the given query expression. pandas.DataFrame.query() grammar DataFrame . query(expr, inplace = False , ** kwargs) parameter expr Filter rows based on query e

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial