Pandas How to Convert DataFrame to Dictionary Easily
to_dict() method on your DataFrame like df.to_dict() to convert it into a dictionary.Examples
How to Think About It
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
Code
import pandas as pd df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]}) dict_result = df.to_dict() print(dict_result)
Dry Run
Let's trace converting a simple DataFrame with columns 'A' and 'B' to a dictionary.
Create DataFrame
df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
Call to_dict()
df.to_dict() converts columns to keys and rows to nested dictionaries
Result
{'A': {0: 1, 1: 2}, 'B': {0: 3, 1: 4}}
| Column | Row 0 | Row 1 |
|---|---|---|
| A | 1 | 2 |
| B | 3 | 4 |
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
import pandas as pd df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]}) dict_list = df.to_dict(orient='list') print(dict_list)
import pandas as pd df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]}) dict_records = df.to_dict(orient='records') print(dict_records)
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| to_dict() default | O(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 |
df.to_dict(orient='list') for a simpler dictionary with column keys and list values.orient when a different dictionary structure is needed, leading to unexpected nested dictionaries.