Convert a Pandas DataFrame to a Dict Without Index in Python

When I was working on a data analytics project, I needed to convert a Pandas DataFrame to a dictionary format without including the index. The issue was that the default conversion methods would include the DataFrame index as part of the dictionary structure, which wasn’t what I needed for my API integration.

In this article, I will cover several simple methods to convert a Pandas DataFrame to a dictionary without the index in Python.

So let’s dive in…

DataFrames and Dictionaries in Python

Before we get into the conversion methods, let’s quickly understand what we’re working with.

A DataFrame is a 2-dimensional labeled data structure in Pandas with columns that can be of different types. It’s similar to a spreadsheet or SQL table.

A dictionary in Python is a collection of key-value pairs that is unordered, changeable, and indexed.

When converting between these two data structures, we need to be careful about how the index is handled.

Read Convert a DataFrame to a Nested Dictionary in Python

Convert a Pandas DataFrame to a Dict Without Index in Python

Now, I will explain how to convert a Pandas DataFrame to a dict without an index in Python.

Method 1: Use to_dict() with orient=’records’

The simplest and most simple way to convert a DataFrame to a dictionary without including the index in Python is by using the to_dict() method with the orient='records' parameter.

Let me demonstrate this with a practical example:

import pandas as pd

# Creating a sample DataFrame
data = {
    'Name': ['John Smith', 'Michael Johnson', 'Sarah Davis'],
    'State': ['New York', 'California', 'Texas'],
    'Age': [32, 45, 28],
    'Salary': [85000, 120000, 72000]
}

df = pd.DataFrame(data)

# Converting DataFrame to dictionary without index
result = df.to_dict(orient='records')

print(result)

The output will be:

[{'Name': 'John Smith', 'State': 'New York', 'Age': 32, 'Salary': 85000}, 
 {'Name': 'Michael Johnson', 'State': 'California', 'Age': 45, 'Salary': 120000}, 
 {'Name': 'Sarah Davis', 'State': 'Texas', 'Age': 28, 'Salary': 72000}]

I executed the above example code and added the screenshot below.

dataframe to dict without index

As you can see, this gives us a list of dictionaries where each dictionary represents a row in the DataFrame, and the index is completely excluded.

Check out Drop Rows in Python Pandas DataFrames

Method 2: Use DataFrame.to_dict() with Different Orient Options

The to_dict() method in Pandas offers several orientation options that result in different dictionary structures in Python. Let’s explore some of them:

1. orient=’list’

Converts the DataFrame into a dictionary with column names as keys and lists of column values as values.

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

Output:

{'Name': ['John Smith', 'Michael Johnson', 'Sarah Davis'], 
 'State': ['New York', 'California', 'Texas'], 
 'Age': [32, 45, 28], 
 'Salary': [85000, 120000, 72000]}

I executed the above example code and added the screenshot below.

pandas to dict without index

This creates a dictionary where each key is a column name and each value is a list of the column values.

2. orient=’dict’

Converts the DataFrame into a nested dictionary with column names as keys and row index–value pairs as inner dictionaries.

dict_dict = df.to_dict(orient='dict')
print(dict_dict)

Output:

{'Name': {0: 'John Smith', 1: 'Michael Johnson', 2: 'Sarah Davis'}, 
 'State': {0: 'New York', 1: 'California', 2: 'Texas'}, 
 'Age': {0: 32, 1: 45, 2: 28}, 
 'Salary': {0: 85000, 1: 120000, 2: 72000}}

This includes the index, so it’s not what we want for our index-free conversion.

Read Use Pandas to Convert Float to Int in Python

Method 3: Use values.tolist() with Column Names

Another approach is to manually construct the dictionary by combining the column names with the values:

# Get column names and values
columns = df.columns.tolist()
values = df.values.tolist()

# Create list of dictionaries
result = []
for row in values:
    result.append(dict(zip(columns, row)))

print(result)

Output:

[{'Name': 'John Smith', 'State': 'New York', 'Age': 32, 'Salary': 85000}, 
 {'Name': 'Michael Johnson', 'State': 'California', 'Age': 45, 'Salary': 120000}, 
 {'Name': 'Sarah Davis', 'State': 'Texas', 'Age': 28, 'Salary': 72000}]

I executed the above example code and added the screenshot below.

pandas to_dict without index

This approach gives you more control over the conversion process, which can be useful in more complex scenarios.

Check out Print the First 10 Rows from a Pandas DataFrame in Python

Method 4: Use DataFrame.apply() with dict() and axis=1

Another elegant way to achieve this conversion is by using the apply() method with axis=1:

# Using apply method with axis=1
result = df.apply(lambda row: row.to_dict(), axis=1).tolist()
print(result)

Output:

[{'Name': 'John Smith', 'State': 'New York', 'Age': 32, 'Salary': 85000}, 
 {'Name': 'Michael Johnson', 'State': 'California', 'Age': 45, 'Salary': 120000}, 
 {'Name': 'Sarah Davis', 'State': 'Texas', 'Age': 28, 'Salary': 72000}]

This method applies the to_dict() function to each row and then converts the result to a list.

Method 5: Use a Dictionary Comprehension

For those who prefer more Pythonic approaches, here’s a one-liner using a Python dictionary comprehension:

# One-liner with dictionary comprehension
result = [{col: df.iloc[i][col] for col in df.columns} for i in range(len(df))]
print(result)

Output:

[{'Name': 'John Smith', 'State': 'New York', 'Age': 32, 'Salary': 85000}, 
 {'Name': 'Michael Johnson', 'State': 'California', 'Age': 45, 'Salary': 120000}, 
 {'Name': 'Sarah Davis', 'State': 'Texas', 'Age': 28, 'Salary': 72000}]

Real-world Example: Process Sales Data for API Integration

Let me show you a more practical example. Let’s say we have a DataFrame containing sales data from different states in the USA, and we need to convert it to a dictionary format for an API integration:

import pandas as pd

# Creating a sample sales DataFrame
sales_data = {
    'Product': ['Laptop', 'Smartphone', 'Tablet', 'Headphones', 'Monitor'],
    'State': ['California', 'New York', 'Texas', 'Florida', 'Illinois'],
    'Units_Sold': [350, 500, 280, 320, 210],
    'Revenue': [525000, 450000, 168000, 96000, 126000],
    'Quarter': ['Q1', 'Q1', 'Q2', 'Q2', 'Q3']
}

sales_df = pd.DataFrame(sales_data)

# Adding an index that we don't want in our final dictionary
sales_df.index = ['A' + str(i) for i in range(len(sales_df))]
print("Original DataFrame with custom index:")
print(sales_df)

# Converting to dictionary without index for API integration
api_data = sales_df.to_dict(orient='records')
print("\nData ready for API (without index):")
print(api_data)

Output:

Original DataFrame with custom index:
    Product      State  Units_Sold  Revenue Quarter
A0   Laptop California        350   525000      Q1
A1  Smartphone   New York        500   450000      Q1
A2    Tablet      Texas        280   168000      Q2
A3  Headphones   Florida        320    96000      Q2
A4   Monitor   Illinois        210   126000      Q3

Data ready for API (without index):
[{'Product': 'Laptop', 'State': 'California', 'Units_Sold': 350, 'Revenue': 525000, 'Quarter': 'Q1'}, 
 {'Product': 'Smartphone', 'State': 'New York', 'Units_Sold': 500, 'Revenue': 450000, 'Quarter': 'Q1'}, 
 {'Product': 'Tablet', 'State': 'Texas', 'Units_Sold': 280, 'Revenue': 168000, 'Quarter': 'Q2'}, 
 {'Product': 'Headphones', 'State': 'Florida', 'Units_Sold': 320, 'Revenue': 96000, 'Quarter': 'Q2'}, 
 {'Product': 'Monitor', 'State': 'Illinois', 'Units_Sold': 210, 'Revenue': 126000, 'Quarter': 'Q3'}]

As you can see, the custom index (A0, A1, etc.) is completely removed from the dictionary, which is exactly what we need for API integration.

Check out Filter DataFrame in Python Pandas

Performance Considerations

When working with larger DataFrames, performance becomes important. Let’s compare the execution times of different methods:

import pandas as pd
import time
import numpy as np

# Create a larger DataFrame
large_df = pd.DataFrame(np.random.randint(0, 100, size=(10000, 5)), 
                        columns=['A', 'B', 'C', 'D', 'E'])

# Method 1: to_dict(orient='records')
start = time.time()
result1 = large_df.to_dict(orient='records')
time1 = time.time() - start

# Method 2: Manual construction
start = time.time()
columns = large_df.columns.tolist()
values = large_df.values.tolist()
result2 = []
for row in values:
    result2.append(dict(zip(columns, row)))
time2 = time.time() - start

# Method 3: apply method
start = time.time()
result3 = large_df.apply(lambda row: row.to_dict(), axis=1).tolist()
time3 = time.time() - start

# Method 4: Dictionary comprehension
start = time.time()
result4 = [{col: large_df.iloc[i][col] for col in large_df.columns} for i in range(len(large_df))]
time4 = time.time() - start

print(f"Method 1 (to_dict): {time1:.5f} seconds")
print(f"Method 2 (manual): {time2:.5f} seconds")
print(f"Method 3 (apply): {time3:.5f} seconds")
print(f"Method 4 (comprehension): {time4:.5f} seconds")

In my tests, the to_dict(orient='records') method is consistently the fastest for most DataFrame sizes, which is why I recommend it as the primary approach.

Read Pandas Count Rows with Condition in Python

When to Use DataFrame to Dict Conversion Without Index

Converting DataFrames to dictionaries without indexes is particularly useful in several scenarios:

  1. API Integration – When you need to send data to an API that expects a specific JSON format
  2. Data Serialization – When saving data to formats like JSON, where the index isn’t meaningful
  3. Web Applications – When preparing data for frontend display (like in Flask or Django apps)
  4. Data Processing Pipelines – When intermediate representations don’t need row identifiers

I hope you found this article helpful for converting Pandas DataFrames to dictionaries without including the index. The to_dict(orient='records') method is the simplest approach, but there are several alternatives if you need more control over the process.

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.