Skip to main content

How to Turn a pandas DataFrame into a Dictionary with to_dict

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

Many libraries and web APIs want plain Python dictionaries, not DataFrames. pandas' to_dict turns a DataFrame into one, and its orient argument chooses the dictionary's shape. This guide converts a small table of cities three ways, with real output from pandas 3.1.1.

Syntax​

Syntax
<dataframe>.to_dict(orient="<shape>")
PartWhat it means
orient="dict"The default. One key per column; each holds {row label: value}
orient="records"A list with one dictionary per row
orient="split"Separate index, columns and data lists. Add index=False to leave the row labels out

The pandas reference: "Convert the DataFrame to a dictionary. The type of the key-value pairs can be customized with the parameters (see below)."

Prerequisites​

  • pandas in your project, for example with uv add pandas. This guide uses pandas 3.1.1 on Python 3.15.2.

Step 1: Write the Script​

frame_to_dict.py
import pandas as pd

cities = pd.DataFrame({"city": ["Paris", "Rome"], "visitors": [120, 95]})

print(cities.to_dict())
print(cities.to_dict(orient="records"))
print(cities.to_dict(orient="split", index=False))

The frame_to_dict.py script in a macOS-style code window: a cities DataFrame with city and visitors columns, then print of to_dict(), to_dict(orient="records") and to_dict(orient="split", index=False).

Step 2: Run It​

Terminal
uv run python frame_to_dict.py
Output
{'city': {0: 'Paris', 1: 'Rome'}, 'visitors': {0: 120, 1: 95}}
[{'city': 'Paris', 'visitors': 120}, {'city': 'Rome', 'visitors': 95}]
{'columns': ['city', 'visitors'], 'data': [['Paris', 120], ['Rome', 95]]}

The script's output in a terminal: the column dictionary with row labels, the list of two row records, and the split dictionary with columns and data.

  • The default keyed everything by column, then by the row labels 0 and 1.
  • records gave one dictionary per city, the shape most web APIs expect.
  • split gave the column names once and the rows as lists, without row labels.

Common Mistakes​

MistakeWhat happensDo this instead
An orient name that doesn't exist, such as "rows"ValueError: orient 'rows' not understoodUse one of the names in the reference: dict, list, series, split, tight, records or index
Leaving out index=False with splitAn extra 'index': [0, 1] list comes firstPass index=False when the row labels mean nothing

Troubleshooting​

SymptomLikely causeFix
The dictionary is nested by row numbers you didn't expectThe default orient="dict"Choose orient="records" for one dictionary per row

FAQ​

Which orient should I use for a web API?​

Whichever the API documents. records matches APIs that take a list of objects; split matches APIs that take column names and rows separately.

Does to_dict change the DataFrame?​

No. It returns a new dictionary: printing cities after to_dict showed the same two rows.

Conclusion​

to_dict() keyed the table by column, orient="records" gave a list of rows, and orient="split", index=False gave columns and rows as separate lists.

Additional Resources​