Python

Pandas to Dictionary

“In Python, a data structure called a dictionary is used to store information as key-value pairs. Dictionary objects are optimized to extract data/values when the key or keys are known. Keep in mind that dictionaries may include duplicate keys. To efficiently find values using the related index, we can convert a pandas series or dataframe with a relevant index into a dictionary object with “index: value” key-value pairs. To achieve this task, the “to_dict()” method can be used. This function is a built-in function found in the pandas module’s Series class. A dataframe is converted into a python list-like data dictionary of series using the pandas.to_dict() method, depending on the specified value of the orient parameter.”

How to Convert Pandas Into a Python Dictionary?

There are multiple methods to convert pandas to a dictionary. However, to transform a Pandas dataframe into a Python dictionary, we’ll use the to_dict() method in Pandas. We can orient the returned dictionary’s key-value pairs in a variety of ways using the to_dict() function. The function’s syntax is as follows:

Syntax

 

pandas.to_dict(orient = “dict”, into=)

 

Parameters

orient: Which datatype to convert columns (series into) is specified by the string value (“dict”, “list”, “records”, “index”, “series”, “split”). For instance, the keyword “list” would give a python dictionary of list objects with the keys “Column name” and “List” (converted series) as output.

into: class, can be passed as an instance or actual class. For instance, a class instance can be passed in the case of a default dict. The parameter’s default value is dict.

Return type: Dictionary converted from a dataframe or series.

Example # 01: Converting the Pandas Dataframe Into a Dictionary

Using the tuple of lists in the pd.DataFrame() function, we will create a basic dataframe with some columns and rows so we can later convert it into a python dictionary.


We have created our dataframe by passing the list inside the pd.DataFrame() function. In the above dataframe, we have three columns “company”, “sales”, and “revenue”. In the column company, we have stored the names of random companies as(“A&B”, “Max_tech”, “XT”, “MJ”, “Quanto”, “Mini_X”, “Zomo”, “AU”, “HL”, “ZMX”, “Earny”), the column “sales” is representing the sales of each company as (“217”, “200”, “199”, “150”, “210”, “216”, “185”, “189”, “202”, “159”, “205”), and the column “revenue” is storing the values representing the revenue of each company against the respective sales(340000   320000  300000 270000  315000  335000  250000     255000   301000   244000 305000). Now we will convert our dataframe “df” into a python dictionary.


By applying the to_dict() method to the df dataframe, we have converted a pandas dataframe into a dictionary.

Example # 02: Converting the Pandas Dataframe Created From a CSV File Into a Dictionary

In example # 1, we created a dataframe using tuples inside the list. Now we will create a dataframe with the help of a CSV file, and then we will convert it into a dictionary using the to_dict() function.


To read a file as a dataframe, we have used the pd.read_csv() function. In the dataframe above, we have two columns (Name and Marks) and seventeen rows (from 0 to 16). Now we will use the method to_dict().


The function has converted our dataframe “df” into a python dictionary.

Example # 03: Convert Pandas Dataframe to the Dictionary Containing the Lists of Values

In the earlier examples, we have converted the pandas into a python dictionary containing multiple dictionaries. When converting a dataframe into a dictionary object, column labels should serve as the dictionary’s keys, and all of the columns’ data or values should be added to the resulting dictionary as a values list for each key.


We have created the dataframe having three columns “name”, “country”, and “age”. In the column “name”, we have stored the data values (“Anna”, “Marty”, “Carl”, “Mary”, “Cleb”, “Ali”, “Alexa”, “Becky”, “Ryan”). While the other columns country and age are strong values as (“USA”, “England”, “USA”, “France”, “Russia”, “Russia”, “France”, “England”, “USA”) and (34, 32, 30, 27, 31, 33, 35, 25, 30) respectively. We’ll create a dictionary containing the lists using the “list” parameter inside the to_dict() method.


By using the list parameter as an argument inside the to_list() function, we have generated a dictionary containing multiple lists.

Example # 03: Convert Pandas Dataframe to the Dictionary Containing the Series of Values

When a DataFrame needs to be transformed into a dictionary, the column name serves as the dictionary keys and the row index and data in the column as a value for the corresponding keys in the dictionary.


We have created the required dataframe using the pd.DataFrame() method. In the recently created dataframe, we have two columns. The name column stores the data values as a string (“Kim”, “Morris”, “Casper”, “Milli”, “Dave”, “Will”, “Billy”), whereas the marks columns consist of numeric data as (8, 9, 6, 7, 10, 7, 8). We will use the parameter “series” as a string inside the to_dict() function.

Example # 04: Convert Pandas Dataframe to the Dictionary Without Index and Header

The parameter “split” of the to_dict() function can be used to extract data from a DataFrame without the headers of columns or when we need to remove the header and row index from the data. Column labels, row index, and actual data are split into three components using this parameter. Let’s create a dataframe, so we can split it into three parts while converting it into the dictionary.


We have created two columns with labels “name” and “age” containing values (“Dave”, “Morris”, “Billy”, “Milli”, “Kim”, “Will”, “Casper”) and (19, 19, 25, 21, 19, 21, 23) respectively. Let’s convert them to python dictionaries.


Using the key “data,” we can retrieve the data from the resultant dictionary without an index or header.

Example # 05: Convert Pandas Dataframe to the Dictionary by Row and Row Index

The parameter “record” can be used inside the to_dict() function to store data of each dataframe row in multiple distinct dictionary objects inside a list or when row-wise data is required. A list containing dictionary objects will be returned. A dictionary with a column label as the key and column data as the value for every row.


We have created a dataframe with columns “name” and “salary”. The “name” column contains the data values (“Leo”, “Haris”, “Wanda”, “Mike”, “Kelly”, “Adam”, “Jack”), and the salary column stores the values (12000, 12500, 14000, 11000, 12000, 13000, 12500). Now let’s create a list with multiple python dictionaries containing data of each row.


The index parameter can also be used to convert each row’s data from a dataframe to a dictionary. A list containing dictionary items will be returned. Each row generates a dictionary. Where the row index will be the key and the value will be the dictionary of data and column label.

Conclusion

In this tutorial, we have discussed how we can convert the dataframe or pandas objects into a python dictionary. We have seen the syntax of the to_dict() function to understand the parameters of this function and how you can modify the function”s output by specifying the function with different parameters. In the examples of this tutorial, we have used the to_dict() method, an inbuilt pandas function, to change the pandas objects to the python dictionary.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.