. Advertisement .
..3..
. Advertisement .
..4..
Not every program requires you to transform data into DataFrames. Sometimes, the opposite direction is what you look for. Read on and learn more about Pandas – Convert DataFrame to dictionary (dict) if that is the case.
Pandas – Convert DataFrame To Dictionary (Dict)
The instance method to_dict() can be used to convert a Pandas DataFrame into a Python dictionary containing list-like data types or Series.
Syntax:
DataFrame.to_dict(orient, into)
Parameters:
- orient: the value of this parameter specifies the type of the values in the resulting dictionary. It must be one of these strings: ‘dict’, ‘series’, ‘list’, ‘index’, ‘records’, and ‘split’. For instance, when orient = ‘list’, your dictionary will consist of lists.
- into: the subclass or instance of collections.abc.Mapping. You will rarely need to use this parameter. However, it is extremely useful when you want to convert a DataFrame to another dictionary type, not the standard dict type of Python.
The following statements create sample DataFrame that we are going to use to show you how the method to_dict() works:
import pandas as pd
headers = ['Site', 'Ranking', 'Type', 'Top Language']
data = [
['ITTutoria', 1, 'Tutorials', 'Python'],
['Stack Overflow', 2, 'Q&A', 'JavaScript'],
['Quora', 3, 'Q&A', None],
['Reddit', 4, 'Forum', None]
]
df = pd.DataFrame(data, columns = headers)
df
......... ADVERTISEMENT .........
..8..
You can apply the method to_dict() to the DataFrame df straight away without the need to give any parameter:
>>> dic = df.to_dict()
>>> pp.pprint(dic)
......... ADVERTISEMENT .........
..8..
As you can see, the returned object (dic) is a dictionary of dictionaries. This is the result of the default value of the parameter orient, which is ‘dict’. In this orientation, each column value of the DataFrame becomes a value inside a sub-dictionary, while the column labels become keys of the returned list.
Not every scenario can benefit from this default orientation, however. For instance, you can change it to the list orientation:
>>> dic = df.to_dict('list')
>> pp.pprint(dic)
......... ADVERTISEMENT .........
..8..
The returned dictionary now contains lists as the value in each pair. The elements of those lists are exactly the items in the original DataFrame’s columns.
To use the Series orientation:
>>> dic = df.to_dict('series')
> pp.pprint(dic)
......... ADVERTISEMENT .........
..8..
The value of each pair in the dictionary has become a Pandas Series. In this orientation, each column in the original DataFrame is converted into a Series instance, which in turn is indexed using the row labels. As with other orientations, the column labels become the keys in the returned dictionary object.
This is how the method to_dict() split the dataset in the DataFrame:
>>> dic = df.to_dict('split')
>> pp.pprint(dic)
......... ADVERTISEMENT .........
..8..
In this orientation, the column labels, indexes, and elements are put into three separate pairs in the returned dictionary object. The key ‘columns’ has a list storing all column labels. The key ‘data’ points to a list of lists wrapping each row in the DataFrame. At the end of the dictionary is the key ‘index’, which holds a list of indexes in the DataFrame.
Here is an example of the records orientation:
>>> dic = df.to_dict('records')
>> pp.pprint(dic)
......... ADVERTISEMENT .........
..8..
While the argument ‘records’ doesn’t return a dictionary, it is still worth a look. Each row in the DataFrame is converted into a dictionary whose keys are the column labels and values are the elements in the row. All the dictionaries then make up the returned list object.
You can see how the index orientation achieves a similar result but returns a dictionary instead of a list:
>>> dic = df.to_dict('index')
>> pp.pprint(dic)
......... ADVERTISEMENT .........
..8..
Each row of the DataFrame also becomes a dictionary like the records orientation. However, these dictionaries then become the values of a holding dictionary whose keys are the indexes of the DataFrame.
Conclusion
As far as Pandas – Convert DataFrame to dictionary (dict) is concerned, the method to_dict() provides a great level of control. You can customize how the returned dictionary is structured through just an argument.
Leave a comment