Series from dict
A dictionary becomes a Series where the keys are the index and the values are the data.
In this page:
Syntax
s = pd.Series({"label1": value1, "label2": value2})
Series from dict
Passing a dict to pd.Series uses keys as index labels and values as data, in insertion order. Supply an index to select or reorder keys; missing keys become NaN. This is a natural way to model labelled data.
Note:
Passing a list of keys as index picks only those keys.
Example: Series from dict
import pandas as pd
ages = {"Ann": 28, "Bob": 35, "Cy": 41}
s = pd.Series(ages)
print(s)
print(pd.Series(ages, index=["Cy", "Ann", "Zed"]))
# Output:
# Ann 28
# Bob 35
# Cy 41
# dtype: int64
# Cy 41.0
# Ann 28.0
# Zed NaN
# dtype: float64
Related Topics
Common Mistakes
- Expecting dict order to be sorted
- Passing an index with keys not in the dict and forgetting NaN
- Unhashable dictionary values
Chapter Summary
- Keys become the index
- Values become the data
- A custom index selects keys
- Missing keys give NaN
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: