Python | Pandas Dataframe.to_dict()

Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.

Pandas .to_dict() method is used to convert a dataframe into a dictionary of series or list like data type depending on orient parameter.

Syntax: DataFrame.to_dict(orient=’dict’, into=)



Parameters:
orient: String value, (‘dict’, ‘list’, ‘series’, ‘split’, ‘records’, ‘index’) Defines which dtype to convert Columns(series into). For example, ‘list’ would return a dictionary of lists with Key=Column name and Value=List (Converted series).
into: class, can pass an actual class or instance. For example in case of defaultdict instance of class can be passed. Default value of this parameter is dict.

Return type: Dataframe converted into Dictionary

To download the data set used in following example, click here.
In the following examples, the data frame used contains data of some NBA players. The image of data frame before any operations is attached below.

Example #1: Default conversion into dictionary of Dictionaries
In this case, no parameter is passed to the to_dict() method. Hence it will convert the dataframe in to a dictionary of dictionaries by default.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas module 
import pandas as pd
  
# reading csv file from url 
   
# dropping null value columns to avoid errors
data.dropna(inplace = True)
  
# converting to dict
data_dict = data.to_dict()
  
# display
data_dict

chevron_right


Output:
As shown in the output image, dictionary of dictionaries was returned by to_dict() method. The key of first dictionary is column name and the column is stored with index as key of 2nd dictionary.

 
Example #2: Converting to dictionary of Series

In this example, ‘series’ is passed to the orient parameter to convert the data frame into Dictionary of Series.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas module 
import pandas as pd
  
# reading csv file from url 
   
# dropping null value columns to avoid errors
data.dropna(inplace = True)
  
# converting to dict
data_dict = data.to_dict('series')
  
# printing datatype of first keys value in dict
print(type(data_dict['Name']))
  
# display
data_dict

chevron_right


Output:
As shown in the output image, Since the type of data_dict[‘Name’] was pandas.core.series.Series, to_dict() returned a dictionary of series.



My Personal Notes arrow_drop_up

Developer in day, Designer at night GSoC 2019 with Python Software Foundation (EOS Design system)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.