Convert DataFrame to Dictionary using Pandas DataFrame to_dict() Method
Last Updated :
12 Jun, 2025
to_dict() converts a Pandas DataFrame into a dictionary. The structure of the resulting dictionary depends on the specified orientation, allowing you to choose how rows and columns are represented. Example:
Python
import pandas as pd
df = pd.DataFrame({
'A': [1, 2, 3],
'B': ['x', 'y', 'z']
})
res = df.to_dict()
print(res)
Output{'A': {0: 1, 1: 2, 2: 3}, 'B': {0: 'x', 1: 'y', 2: 'z'}}
Explanation: Each column is converted into a nested dictionary where the keys are the row indices and values are the column entries.
Syntax
DataFrame.to_dict(orient='dict', into=dict)
Parameters:
- orient (str, default='dict') specifies the format of the resulting dictionary. Common options include:
orient | Description | Example |
---|
dict (default) | Dict of columns mapping to dicts of index:value pairs | {column -> {index -> value}} |
---|
list | Dict of columns mapping to lists of values | {column -> [values]} |
---|
series | Dict of columns mapping to Pandas Series | {column -> Series(values)} |
---|
split | Dict containing keys 'index', 'columns', and 'data' | {'index': [...], 'columns': [...], 'data': [...]} |
---|
records | List of dictionaries, each representing a row | [{'col1': val1, 'col2': val2}, ...] |
---|
index | Dict of index labels mapping to dicts of column:value pairs | {index -> {column -> value}} |
---|
- into (class, default=dict) is the collection type used for the resulting dictionary. By default, it is the built-in Python dict, but can be set to collections.OrderedDict or others if desired.
Returns: A dictionary (or specified mapping type) representation of the DataFrame
Examples
Example 1: In this example, we convert the DataFrame into a dictionary where each column name maps to a list of its values.
Python
import pandas as pd
df = pd.DataFrame({
'A': [1, 2, 3],
'B': ['x', 'y', 'z']
})
res = df.to_dict(orient='list')
print(res)
Output{'A': [1, 2, 3], 'B': ['x', 'y', 'z']}
Example 2: In this example, we convert the DataFrame into a list of dictionaries, where each dictionary represents a row with column names as keys.
Python
import pandas as pd
df = pd.DataFrame({
'A': [1, 2, 3],
'B': ['x', 'y', 'z']
})
res = df.to_dict(orient='records')
print(res)
Output[{'A': 1, 'B': 'x'}, {'A': 2, 'B': 'y'}, {'A': 3, 'B': 'z'}]
Example 3: In this example, we convert the DataFrame into a dictionary keyed by the row index, where each value is another dictionary representing the row’s data.
Python
import pandas as pd
df = pd.DataFrame({
'A': [1, 2, 3],
'B': ['x', 'y', 'z']
})
res = df.to_dict(orient='index')
print(res)
Output{0: {'A': 1, 'B': 'x'}, 1: {'A': 2, 'B': 'y'}, 2: {'A': 3, 'B': 'z'}}
Example 4: In this example, the DataFrame is converted into a dictionary with separate keys for the index, columns and data.
Python
import pandas as pd
df = pd.DataFrame({
'A': [1, 2, 3],
'B': ['x', 'y', 'z']
})
res = df.to_dict(orient='split')
print(res)
Output{'index': [0, 1, 2], 'columns': ['A', 'B'], 'data': [[1, 'x'], [2, 'y'], [3, 'z']]}
Similar Reads
How to convert Dictionary to Pandas Dataframe? Converting a dictionary into a Pandas DataFrame is simple and effective. You can easily convert a dictionary with key-value pairs into a tabular format for easy data analysis. Lets see how we can do it using various methods in Pandas.1. Using the Pandas ConstructorWe can convert a dictionary into Da
2 min read
Pandas Dataframe.to_numpy() - Convert dataframe to Numpy array to_numpy() method converts Pandas DataFrame into a NumPy array. This method is used when we need to perform numerical operations that require a NumPy array instead of a DataFrame structure. In this article, we will see how to convert a dataframe to anumpy array.Syntax: Dataframe.to_numpy(dtype = Non
3 min read
Python - Convert dict of list to Pandas dataframe In this article, we will discuss how to convert a dictionary of lists to a pandas dataframe. Method 1: Using DataFrame.from_dict() We will use the from_dict method. This method will construct DataFrame from dict of array-like or dicts. Syntax: pandas.DataFrame.from_dict(dictionary) where dictionary
2 min read
How to create DataFrame from dictionary in Python-Pandas? The task of converting a dictionary into a Pandas DataFrame involves transforming a dictionary into a structured, tabular format where keys represent column names or row indexes and values represent the corresponding data.Using Default ConstructorThis is the simplest method where a dictionary is dir
3 min read
Create Pandas Dataframe from Dictionary of Dictionaries In this article, we will discuss how to create a pandas dataframe from the dictionary of dictionaries in Python. Method 1: Using DataFrame() We can create a dataframe using Pandas.DataFrame() method. Syntax: pandas.DataFrame(dictionary) where pandas are the module that supports DataFrame data struct
2 min read
Convert PySpark DataFrame to Dictionary in Python In this article, we are going to see how to convert the PySpark data frame to the dictionary, where keys are column names and values are column values. Before starting, we will create a sample Dataframe: Python3 # Importing necessary libraries from pyspark.sql import SparkSession # Create a spark se
3 min read
How to convert pandas DataFrame into JSON in Python? We are given a pandas DataFrame, and our task is to convert it into JSON format using different orientations and custom options. JSON (JavaScript Object Notation) is a lightweight, human-readable format used for data exchange. With Pandas, this can be done easily using the to_json() method. For exam
4 min read
Convert Python Dictionary List to PySpark DataFrame In this article, we will discuss how to convert Python Dictionary List to Pyspark DataFrame. It can be done in these ways: Using Infer schema.Using Explicit schemaUsing SQL Expression Method 1: Infer schema from the dictionary We will pass the dictionary directly to the createDataFrame() method. Syn
3 min read
Python | Convert list of nested dictionary into Pandas dataframe Given a list of the nested dictionary, write a Python program to create a Pandas dataframe using it. We can convert list of nested dictionary into Pandas DataFrame. Let's understand the stepwise procedure to create a Pandas Dataframe using the list of nested dictionary. Convert Nested List of Dictio
4 min read
How To Convert Sklearn Dataset To Pandas Dataframe In Python In this article, we look at how to convert sklearn dataset to a pandas dataframe in Python. Sklearn and pandas are python libraries that are used widely for data science and machine learning operations. Pandas is majorly focused on data processing, manipulation, cleaning, and visualization whereas s
3 min read