← Back to Pandas Course | Chapter 2: Series | Lesson 7 of 7

Series from dict

A dictionary becomes a Series where the keys are the index and the values are the data.

In this page:

  1. Series from dict
Syntax
python
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

python
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
  1. Expecting dict order to be sorted
  2. Passing an index with keys not in the dict and forgetting NaN
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.