Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 02

Creating a Series

Build a Series from a list, a dict, an ndarray, or a single value, and set your own index, dtype, and name while you do it.

Intermediate25 min
Creating a Series hero Module 24 Lesson 2Creating a Series hero Module 24 Lesson 2

pd.Series(data, index, dtype, name) builds one labelled column. The data can be a list, a tuple, a dict, a NumPy array, or a single value. A set will not work, because a set has no order.

A three-value Series with day labels as its indexA three-value Series with day labels as its index

From a list

The default index counts from 0. pandas picks the dtype from the values, and you can ask for a different one.

python
import pandas as pd

counts = pd.Series([10, 20, 30, 40])
print(counts)
print(counts.dtype)

as_float = pd.Series([10, 20, 30, 40], dtype=float)
print(as_float)

Output:

text
0    10
1    20
2    30
3    40
dtype: int64
int64
0    10.0
1    20.0
2    30.0
3    40.0
dtype: float64

Your own index

Pass index with one label per value. Labels are usually text, but they can be numbers, and they do not have to be unique. Once labels exist, you can look a value up by label.

python
import pandas as pd

profile = pd.Series([7, "Avery", 88.5], index=["id", "name", "score"])
print(profile)
print(profile["name"])

codes = pd.Series(["north", "east", "west"], index=[100, 200, 300])
print(codes)
print(codes[200])

Output:

text
id           7
name     Avery
score     88.5
dtype: object
Avery
100    north
200     east
300     west
dtype: object
east

Mixed types give dtype: object, because one Series holds one type. A Series of mixed values is legal but slow, so keep one kind of value per Series when you can.

From a dict

The keys become the index and the values become the data, so you do not pass index at all. If you do pass one, pandas keeps only those labels and marks any missing label as NaN.

python
import pandas as pd

fees = {"python": 0, "numpy": 0, "pandas": 0, "workshop": 49}
plan = pd.Series(fees)
print(plan)

picked = pd.Series(fees, index=["pandas", "workshop", "sql"])
print(picked)

Output:

text
python       0
numpy        0
pandas       0
workshop    49
dtype: int64
pandas       0.0
workshop    49.0
sql          NaN
dtype: float64

From an array or a single value

A NumPy array becomes a Series without copying the numbers. A single value is repeated for every label you pass, which is a quick way to start a column of defaults.

python
import numpy as np
import pandas as pd

readings = pd.Series(np.arange(5, 9))
print(readings)

defaults = pd.Series(0, index=["mon", "tue", "wed"])
print(defaults)

Output:

text
0    5
1    6
2    7
3    8
dtype: int64
mon    0
tue    0
wed    0
dtype: int64

Name the Series

A Series can carry a name. When it becomes a DataFrame column later, that name is the column heading.

python
import pandas as pd

hours = pd.Series([42, 31, 55], index=["Avery", "Jordan", "Casey"], name="hours")
print(hours)
print(hours.name)
print(hours.to_frame())

Output:

text
Avery     42
Jordan    31
Casey     55
Name: hours, dtype: int64
hours
        hours
Avery      42
Jordan     31
Casey      55

Key takeaways

  • pd.Series(data, index, dtype, name) builds one labelled column

  • A dict turns its keys into the index

  • A missing label becomes NaN

  • Mixed values fall back to dtype: object

  • Next: Series Attributes

  • Previous: What Is pandas

Loading playground…

Check your understanding

Creating a Series Quiz

What does pd.Series([10, 20, 30]) use as its index?

1 / 10