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.
.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=)
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.
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.
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.
Last Updated on November 13, 2021 by admin