0
0
PandasHow-ToBeginner · 2 min read

Pandas How to Convert DataFrame to Dictionary Easily

Use the pandas to_dict() method on your DataFrame like df.to_dict() to convert it into a dictionary.
📋

Examples

Inputdf = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
Output{'A': {0: 1, 1: 2}, 'B': {0: 3, 1: 4}}
Inputdf = pd.DataFrame({'Name': ['Alice', 'Bob'], 'Age': [25, 30]})
Output{'Name': {0: 'Alice', 1: 'Bob'}, 'Age': {0: 25, 1: 30}}
Inputdf = pd.DataFrame()
Output{}
🧠

How to Think About It

To convert a DataFrame to a dictionary, think of the DataFrame as a table with rows and columns. The to_dict() method transforms this table into a dictionary where keys can be columns or rows depending on the chosen format. By default, it creates a dictionary with column names as keys and dictionaries of row indices and values as values.
📐

Algorithm

1
Get the input DataFrame.
2
Call the <code>to_dict()</code> method on the DataFrame.
3
Return the resulting dictionary.
💻

Code

python
import pandas as pd

df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
dict_result = df.to_dict()
print(dict_result)
Output
{'A': {0: 1, 1: 2}, 'B': {0: 3, 1: 4}}
🔍

Dry Run

Let's trace converting a simple DataFrame with columns 'A' and 'B' to a dictionary.

1

Create DataFrame

df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})

2

Call to_dict()

df.to_dict() converts columns to keys and rows to nested dictionaries

3

Result

{'A': {0: 1, 1: 2}, 'B': {0: 3, 1: 4}}

ColumnRow 0Row 1
A12
B34
💡

Why This Works

Step 1: DataFrame Structure

A DataFrame is like a table with rows and columns, where each column has a name and values.

Step 2: to_dict() Method

The to_dict() method converts the DataFrame into a dictionary where keys are column names and values are dictionaries of row indices and their corresponding values.

Step 3: Default Format

By default, to_dict() uses the 'dict' format, which nests row data inside each column key.

🔄

Alternative Approaches

to_dict with 'list' orientation
python
import pandas as pd
df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
dict_list = df.to_dict(orient='list')
print(dict_list)
This returns a dictionary with column names as keys and lists of column values, which is simpler for some uses.
to_dict with 'records' orientation
python
import pandas as pd
df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
dict_records = df.to_dict(orient='records')
print(dict_records)
This returns a list of dictionaries, each representing a row, useful for row-wise processing.

Complexity: O(n*m) time, O(n*m) space

Time Complexity

Converting a DataFrame to a dictionary requires visiting each cell once, so time grows with the number of rows (n) times columns (m).

Space Complexity

The output dictionary stores all data, so space also grows with the size of the DataFrame.

Which Approach is Fastest?

All to_dict() orientations have similar time complexity; choose based on the desired dictionary shape.

ApproachTimeSpaceBest For
to_dict() defaultO(n*m)O(n*m)Nested dict by columns and rows
to_dict(orient='list')O(n*m)O(n*m)Simple dict with lists per column
to_dict(orient='records')O(n*m)O(n*m)List of dicts, one per row
💡
Use df.to_dict(orient='list') for a simpler dictionary with column keys and list values.
⚠️
Forgetting to specify orient when a different dictionary structure is needed, leading to unexpected nested dictionaries.