Convert Dictionary to Series in pandas - Simple Guide
pandas.Series(your_dict) to convert a dictionary to a pandas Series, where keys become the index and values become the data.Examples
How to Think About It
Algorithm
Code
import pandas as pd dict_data = {'a': 1, 'b': 2, 'c': 3} series = pd.Series(dict_data) print(series)
Dry Run
Let's trace converting {'a': 1, 'b': 2, 'c': 3} to a Series.
Input dictionary
{'a': 1, 'b': 2, 'c': 3}
Create Series
pd.Series({'a': 1, 'b': 2, 'c': 3})
Output Series
a 1 b 2 c 3 dtype: int64
| Index | Value |
|---|---|
| a | 1 |
| b | 2 |
| c | 3 |
Why This Works
Step 1: Dictionary keys become index
When you pass a dictionary to pd.Series(), pandas uses the dictionary keys as the Series index labels.
Step 2: Dictionary values become data
The values in the dictionary become the data points in the Series.
Step 3: Series object created
The result is a Series object with labeled data, easy to use for analysis.
Alternative Approaches
import pandas as pd dict_data = {'a': 1, 'b': 2, 'c': 3} series = pd.Series.from_dict(dict_data) print(series)
import pandas as pd dict_data = {'a': 1, 'b': 2, 'c': 3} df = pd.DataFrame(list(dict_data.items()), columns=['index', 'value']) series = pd.Series(df['value'].values, index=df['index']) print(series)
Complexity: O(n) time, O(n) space
Time Complexity
Creating a Series from a dictionary requires iterating over all key-value pairs once, so it is O(n) where n is the number of items.
Space Complexity
The Series stores all data points and index labels, so space is O(n) proportional to the dictionary size.
Which Approach is Fastest?
Using pd.Series(dict) or pd.Series.from_dict() are equally fast and efficient. Converting via DataFrame is slower and uses more memory.
| Approach | Time | Space | Best For |
|---|---|---|---|
| pd.Series(dict) | O(n) | O(n) | Simple, direct conversion |
| pd.Series.from_dict() | O(n) | O(n) | Explicit dictionary to Series conversion |
| Dict to DataFrame to Series | O(n) | O(n) | When intermediate data manipulation is needed |
pd.Series(your_dict) directly for a quick and clean conversion.