Open In App

Convert DataFrame to Dictionary using Pandas DataFrame to_dict() Method

Last Updated : 12 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

to_dict() converts a Pandas DataFrame into a dictionary. The structure of the resulting dictionary depends on the specified orientation, allowing you to choose how rows and columns are represented. Example:

Python
import pandas as pd
df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': ['x', 'y', 'z']
})

res = df.to_dict()
print(res)

Output
{'A': {0: 1, 1: 2, 2: 3}, 'B': {0: 'x', 1: 'y', 2: 'z'}}

Explanation: Each column is converted into a nested dictionary where the keys are the row indices and values are the column entries.

Syntax

DataFrame.to_dict(orient='dict', into=dict)

Parameters:

  • orient (str, default='dict') specifies the format of the resulting dictionary. Common options include:

orient

Description

Example

dict (default)

Dict of columns mapping to dicts of index:value pairs

{column -> {index -> value}}

list

Dict of columns mapping to lists of values

{column -> [values]}

series

Dict of columns mapping to Pandas Series

{column -> Series(values)}

split

Dict containing keys 'index', 'columns', and 'data'

{'index': [...], 'columns': [...], 'data': [...]}

records

List of dictionaries, each representing a row

[{'col1': val1, 'col2': val2}, ...]

index

Dict of index labels mapping to dicts of column:value pairs

{index -> {column -> value}}

  • into (class, default=dict) is the collection type used for the resulting dictionary. By default, it is the built-in Python dict, but can be set to collections.OrderedDict or others if desired.

Returns: A dictionary (or specified mapping type) representation of the DataFrame

Examples

Example 1: In this example, we convert the DataFrame into a dictionary where each column name maps to a list of its values.

Python
import pandas as pd
df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': ['x', 'y', 'z']
})

res = df.to_dict(orient='list')
print(res)

Output
{'A': [1, 2, 3], 'B': ['x', 'y', 'z']}

Example 2: In this example, we convert the DataFrame into a list of dictionaries, where each dictionary represents a row with column names as keys.

Python
import pandas as pd
df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': ['x', 'y', 'z']
})

res = df.to_dict(orient='records')
print(res)

Output
[{'A': 1, 'B': 'x'}, {'A': 2, 'B': 'y'}, {'A': 3, 'B': 'z'}]

Example 3: In this example, we convert the DataFrame into a dictionary keyed by the row index, where each value is another dictionary representing the row’s data.

Python
import pandas as pd
df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': ['x', 'y', 'z']
})

res = df.to_dict(orient='index')
print(res)

Output
{0: {'A': 1, 'B': 'x'}, 1: {'A': 2, 'B': 'y'}, 2: {'A': 3, 'B': 'z'}}

Example 4: In this example, the DataFrame is converted into a dictionary with separate keys for the index, columns and data.

Python
import pandas as pd
df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': ['x', 'y', 'z']
})

res = df.to_dict(orient='split')
print(res)

Output
{'index': [0, 1, 2], 'columns': ['A', 'B'], 'data': [[1, 'x'], [2, 'y'], [3, 'z']]}



Next Article

Similar Reads