Back to_dict() : : Pandas method:: convert data into Python dictionary format. 12 Jan, 2026

📘 to_dict() — Simple & Clear Explanation (Pandas)

to_dict() is a Pandas method used to convert data into Python dictionary format.

It is mainly used with:

  • DataFrame

  • Series


🔹 Why do we need to_dict()?

Because:

  • Python APIs, JSON, loops, and logic work better with dictionaries

  • Databases, FastAPI, Flask, ML pipelines often expect dict input

  • Easy to store, send, or manipulate data


1️⃣ to_dict() with Series

Example:

import pandas as pd

s = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
print(s.to_dict())

Output:

{'a': 10, 'b': 20, 'c': 30}

🧠 Meaning
Index → key
Value → value


2️⃣ to_dict() with DataFrame (MOST IMPORTANT)

Example DataFrame:

df = pd.DataFrame({
    'Name': ['Ram', 'Shyam'],
    'Age': [25, 30]
})

3️⃣ Different orient options (Very Important)

🔹 orient='dict' (default)

df.to_dict()
{
 'Name': {0: 'Ram', 1: 'Shyam'},
 'Age': {0: 25, 1: 30}
}

✔ Column-wise dictionary


🔹 orient='records' ⭐ (Most Used in APIs & ML)

df.to_dict(orient='records')
[
 {'Name': 'Ram', 'Age': 25},
 {'Name': 'Shyam', 'Age': 30}
]

🧠 Real-life use:

  • API response

  • JSON

  • Database insert


🔹 orient='list'

df.to_dict(orient='list')
{
 'Name': ['Ram', 'Shyam'],
 'Age': [25, 30]
}

✔ Used in analytics & plotting


🔹 orient='index'

df.to_dict(orient='index')
{
 0: {'Name': 'Ram', 'Age': 25},
 1: {'Name': 'Shyam', 'Age': 30}
}

✔ Row-wise with index as key


🔹 orient='split'

df.to_dict(orient='split')
{
 'index': [0, 1],
 'columns': ['Name', 'Age'],
 'data': [['Ram', 25], ['Shyam', 30]]
}

✔ Used internally in ML pipelines


🧠 Easy Real-World Analogy

Think of Excel → Python

Excel SheetPython Dict
ColumnsDictionary keys
RowsDictionary values
TableNested dictionary

🎯 When to use which?

Use caseBest orient
API / JSONrecords
Analyticslist
Row-based opsindex
ML internalsplit

🚀 One-liner Summary

to_dict() converts Pandas data into Python dictionaries for easy use in logic, APIs, ML & GenAI pipelines.