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.
- Python | pandas.map()
- Python | Pandas Index.contains()
- Python | Pandas Series.ptp()
- Python | Pandas DataFrame.abs()
- Python | Pandas dataframe.all()
- Python | Pandas dataframe.add()
- Python | Pandas Series.pop()
- Python | Pandas Series.mod()
- Python | Pandas Dataframe.at[ ]
- Python | Pandas Dataframe.iat[ ]
- Python | Pandas dataframe.cov()
- Python | Pandas dataframe.get()
- Python | Pandas dataframe.eq()
- Python | Pandas Series.take()
- Python | Pandas Series.std()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.