Convert Dictionary to DataFrame in pandas - Simple Guide
pd.DataFrame(your_dict) to convert a dictionary to a pandas DataFrame directly, where your_dict is the dictionary you want to convert.Examples
How to Think About It
Algorithm
Code
import pandas as pd dict_data = {"A": [1, 2, 3], "B": [4, 5, 6]} df = pd.DataFrame(dict_data) print(df)
Dry Run
Let's trace the example dictionary {"A": [1, 2, 3], "B": [4, 5, 6]} through the code
Input dictionary
{"A": [1, 2, 3], "B": [4, 5, 6]}
Create DataFrame
pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
Output DataFrame
A B 0 1 4 1 2 5 2 3 6
| Index | A | B |
|---|---|---|
| 0 | 1 | 4 |
| 1 | 2 | 5 |
| 2 | 3 | 6 |
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
import pandas as pd dict_data = {"A": 1, "B": 2} series = pd.Series(dict_data) df = series.to_frame().T print(df)
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)
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| pd.DataFrame(dict) | O(n) | O(n) | Standard dictionary with list values |
| pd.Series + to_frame().T | O(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 |