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.to_markdown() in Pandas
- Python | pandas.map()
- Python | Pandas Series.sum()
- Python | Pandas Series.agg()
- Python | Pandas dataframe.sub()
- Python | Pandas TimedeltaIndex.name
- Python | Pandas dataframe.mad()
- Python | Pandas dataframe.max()
- Python | Pandas Series.str.contains()
- Python | Pandas dataframe.mean()
- Python | Pandas Series.div()
- Python | Pandas Series.pow()
- Python | Pandas Panel.mod()
- Python | Pandas Panel.div()
- Python | Pandas Timestamp.day
- Python | Pandas Panel.mul()
- Python | Pandas Series.lt()
- Python | Pandas Panel.sub()
- Python | Pandas Panel.pow()
- Python | Pandas dataframe.div()
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.