Convert DataFrame to Dictionary – DataFrame.to_dict()
To convert DataFrame to a dictionary in Pandas, call to_dict() on this DataFrame. We may specify the type of values in the dictionary via parameters to to_dict() method.
In this tutorial, we will learn the syntax of DataFrame.to_dict() method and how to use this method to convert a given DataFrame into Dictionary object.
Syntax
The syntax of pandas DataFrame to dict() method is
DataFrame.to_dict(orient='dict', into=<class 'dict'>)
where
Parameter | Value | Description |
---|---|---|
orient | str {‘dict’, ‘list’, ‘series’, ‘split’, ‘records’, ‘index’}. default value is ‘dict’. | Determines the type of the values of the dictionary. ‘dict’ (default) : dict like {column -> {index -> value}} ‘list’ : dict like {column -> [values]} ‘series’ : dict like {column -> Series(values)} ‘split’ : dict like {‘index’ -> [index], ‘columns’ -> [columns], ‘data’ -> [values]} ‘records’ : list like [{column -> value}, … , {column -> value}] ‘index’ : dict like {index -> {column -> value}} [Abbreviations are allowed. s indicates series and sp indicates split.] |
into | class. default value is ‘dict’ | The collections.abc.Mapping subclass used for all Mappings in the return value. Can be the actual class or an empty instance of the mapping type you want. If you want a collections.defaultdict, you must pass it initialized. |
Return Value
dict, list or collections.abc.Mapping Return a collections.abc.Mapping object representing the DataFrame. The resulting transformation depends on the orient parameter.
Examples
Convert DataFrame to Dictionary with Default Values for Parameters to to_dict()
In the following program, we take a Dictionary and convert it to dictionary. We do not pass any arguments to to_dict() method and let the method take default values for the parameters.
The resulting dictionary would be of the form {column -> {index -> value}}.
Example.py
import pandas as pd
data = {'name': ["apple", "banana", "cherry"], 'quant': [40, 50, 60]}
df = pd.DataFrame(data)
result = df.to_dict()
print(result)
Output
{'name': {0: 'apple', 1: 'banana', 2: 'cherry'}, 'quant': {0: 40, 1: 50, 2: 60}}
Convert to Dictionary of form {column -> [values]}
To convert DataFrame to dictionary of form {column -> [values]}, pass the value ‘list’ for orient
parameter to to_dict() method.
Example.py
import pandas as pd
data = {'name': ["apple", "banana", "cherry"], 'quant': [40, 50, 60]}
df = pd.DataFrame(data)
result = df.to_dict(orient='list')
print(result)
Output
{'name': ['apple', 'banana', 'cherry'], 'quant': [40, 50, 60]}
Convert to Dictionary of form {‘index’ -> [index], ‘columns’ -> [columns], ‘data’ -> [values]}
To convert DataFrame to dictionary of form {‘index’ -> [index], ‘columns’ -> [columns], ‘data’ -> [values]}, pass the value ‘split’ for orient
parameter to to_dict() method.
Example.py
import pandas as pd
data = {'name': ["apple", "banana", "cherry"], 'quant': [40, 50, 60]}
df = pd.DataFrame(data)
result = df.to_dict(orient='split')
print(result)
Output
{'index': [0, 1, 2], 'columns': ['name', 'quant'], 'data': [['apple', 40], ['banana', 50], ['cherry', 60]]}
Conclusion
In this Pandas Tutorial, we learned how to convert a DataFrame to dictionary using pandas DataFrame.to_dict() method.