0
0
PandasHow-ToBeginner · 2 min read

Convert Dictionary to Series in pandas - Simple Guide

Use pandas.Series(your_dict) to convert a dictionary to a pandas Series, where keys become the index and values become the data.
📋

Examples

Input{"a": 1, "b": 2, "c": 3}
Outputa 1 b 2 c 3 dtype: int64
Input{"x": 10, "y": 20}
Outputx 10 y 20 dtype: int64
Input{}
OutputSeries([], dtype: float64)
🧠

How to Think About It

To convert a dictionary to a Series, think of the dictionary keys as labels (index) and the values as the data points. The pandas Series constructor takes the dictionary and automatically assigns keys as the index and values as the Series data.
📐

Algorithm

1
Get the input dictionary.
2
Pass the dictionary to the pandas Series constructor.
3
Return the created Series with dictionary keys as index and values as data.
💻

Code

python
import pandas as pd

dict_data = {'a': 1, 'b': 2, 'c': 3}
series = pd.Series(dict_data)
print(series)
Output
a 1 b 2 c 3 dtype: int64
🔍

Dry Run

Let's trace converting {'a': 1, 'b': 2, 'c': 3} to a Series.

1

Input dictionary

{'a': 1, 'b': 2, 'c': 3}

2

Create Series

pd.Series({'a': 1, 'b': 2, 'c': 3})

3

Output Series

a 1 b 2 c 3 dtype: int64

IndexValue
a1
b2
c3
💡

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

Using pd.Series.from_dict()
python
import pandas as pd

dict_data = {'a': 1, 'b': 2, 'c': 3}
series = pd.Series.from_dict(dict_data)
print(series)
This method is similar but explicitly shows the intent to create a Series from a dictionary.
Convert dictionary to DataFrame then to Series
python
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)
More complex and less efficient, but useful if you want to manipulate data before creating the 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.

ApproachTimeSpaceBest 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 SeriesO(n)O(n)When intermediate data manipulation is needed
💡
Use pd.Series(your_dict) directly for a quick and clean conversion.
⚠️
Forgetting that dictionary keys become the Series index, not the data values.