0
0
PandasHow-ToBeginner · 2 min read

Convert Dictionary to DataFrame in pandas - Simple Guide

Use pd.DataFrame(your_dict) to convert a dictionary to a pandas DataFrame directly, where your_dict is the dictionary you want to convert.
📋

Examples

Input{"A": [1, 2, 3], "B": [4, 5, 6]}
Output A B 0 1 4 1 2 5 2 3 6
Input{"Name": ["Alice", "Bob"], "Age": [25, 30]}
Output Name Age 0 Alice 25 1 Bob 30
Input{"X": 10, "Y": 20}
Output X Y 0 10 20
🧠

How to Think About It

To convert a dictionary to a DataFrame, think about the dictionary keys as column names and the values as the data for those columns. If the values are lists or arrays, each element corresponds to a row. If the values are single items, pandas will treat them as one-row data.
📐

Algorithm

1
Get the input dictionary.
2
Pass the dictionary to the pandas DataFrame constructor.
3
Return the created DataFrame.
💻

Code

python
import pandas as pd

dict_data = {"A": [1, 2, 3], "B": [4, 5, 6]}
df = pd.DataFrame(dict_data)
print(df)
Output
A B 0 1 4 1 2 5 2 3 6
🔍

Dry Run

Let's trace the example dictionary {"A": [1, 2, 3], "B": [4, 5, 6]} through the code

1

Input dictionary

{"A": [1, 2, 3], "B": [4, 5, 6]}

2

Create DataFrame

pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})

3

Output DataFrame

A B 0 1 4 1 2 5 2 3 6

IndexAB
014
125
236
💡

Why This Works

Step 1: Dictionary keys become columns

When you pass a dictionary to pd.DataFrame(), the keys become the column names of the DataFrame.

Step 2: Values become rows

The values in the dictionary, if they are lists or arrays, become the rows under each column.

Step 3: Single values become one-row DataFrame

If the dictionary values are single items, pandas treats them as one row with those values.

🔄

Alternative Approaches

Using pd.Series and then pd.DataFrame
python
import pandas as pd

dict_data = {"A": 1, "B": 2}
series = pd.Series(dict_data)
df = series.to_frame().T
print(df)
This method is useful when dictionary values are single items and you want a one-row DataFrame; it is less direct but flexible.
Using from_dict with orient='index'
python
import pandas as pd

dict_data = {"row1": {"A": 1, "B": 2}, "row2": {"A": 3, "B": 4}}
df = pd.DataFrame.from_dict(dict_data, orient='index')
print(df)
This approach is good when your dictionary keys represent row labels and values are dictionaries of column data.

Complexity: O(n) time, O(n) space

Time Complexity

Creating a DataFrame from a dictionary requires iterating over all elements once, so it is O(n) where n is the total number of data points.

Space Complexity

The DataFrame stores a copy of the data, so space complexity is O(n) proportional to the input size.

Which Approach is Fastest?

Using pd.DataFrame() directly is the fastest and simplest for typical dictionaries; alternatives add overhead or are for special cases.

ApproachTimeSpaceBest For
pd.DataFrame(dict)O(n)O(n)Standard dictionary with list values
pd.Series + to_frame().TO(n)O(n)Single-row dictionary with scalar values
pd.DataFrame.from_dict with orient='index'O(n)O(n)Nested dictionaries with row labels
💡
Make sure dictionary values are lists of equal length for proper DataFrame columns.
⚠️
Passing dictionary values of different lengths causes errors or unexpected DataFrame shapes.