Creating a Series
Build a Pandas Series from a Python list, a dict, and a NumPy array, and inspect its index and values attributes.
What Is a Pandas Series?
A Pandas Series is a one-dimensional labelled array capable of holding any data type — integers, floats, strings, Python objects, or even other Series. Think of it as a NumPy array with a custom index (labels for each element). The index is what makes Series much more powerful than plain arrays for data analysis tasks where values need descriptive names.
import pandas as pd
import numpy as np
s = pd.Series([10, 20, 30, 40])
print(s)
# 0 10
# 1 20
# 2 30
# 3 40
# dtype: int64Creating a Series from a Python List
Passing a Python list to pd.Series() creates a Series with a default integer RangeIndex (0, 1, 2, ...). You can provide custom index labels via the index= parameter. The length of the index list must match the length of the data. Labels can be strings, integers, dates, or any hashable Python objects.
import pandas as pd
temps = pd.Series([22.5, 19.0, 25.3, 17.8],
index=['Mon', 'Tue', 'Wed', 'Thu'])
print(temps)
# Mon 22.5
# Tue 19.0
# Wed 25.3
# Thu 17.8
# dtype: float64