Series
A Series is the simplest data structure in Pandas - a single column of data paired with a matching index that labels every value. Almost everything else in Pandas, including the DataFrame, is built on top of it.
Creating a Series
A Series can be created from a list, a NumPy array, or a dictionary. If no index is supplied, Pandas automatically assigns a default numeric index starting at 0.
import pandas as pd
marks = pd.Series([88, 76, 91, 64])
print(marks)
# 0 88
# 1 76
# 2 91
# 3 64
# dtype: int64
Using a Custom Index
subjects = pd.Series([88, 76, 91], index=["Maths", "Science", "English"])
print(subjects["Science"]) # 76
print(subjects.index) # Index(['Maths', 'Science', 'English'], dtype='object')
Creating a Series from a Dictionary
city_population = pd.Series({"Delhi": 32900000, "Mumbai": 20700000, "Noida": 700000})
print(city_population["Noida"]) # 700000
Vectorised Operations
Just like NumPy arrays, Series support fast element-wise operations without writing an explicit loop.
bonus = subjects + 5
print(bonus)
# Maths 93
# Science 81
# English 96
# dtype: int64
Coming Up Next
Next, you'll look at the DataFrame in more detail - how multiple Series come together to form a full two-dimensional table.
Selecting Values with loc, iloc and Boolean Masks
A Series offers two ways of picking values, and mixing them up is one of the most common beginner bugs. Use .loc for labels and .iloc for positions.
import pandas as pd
s = pd.Series([100, 200, 300], index=[2, 0, 1])
print(s.loc[0]) # 200 -> the value whose label is 0
print(s.iloc[0]) # 100 -> the first value by position
print(s.iloc[-1]) # 300 -> the last value by position
When the index is made of integers, plain square brackets such as s[0] look up the label, not the position. Writing .loc or .iloc explicitly removes the guesswork.
Boolean masks let you filter without a loop. A comparison returns a Series of True and False, which you then use to select:
marks = pd.Series([88, 76, 91, 64], index=["Asha", "Ravi", "Meera", "Kabir"])
print(marks[marks > 80])
# Asha 88
# Meera 91
print(marks[(marks >= 70) & (marks < 90)])
# Asha 88
# Ravi 76
Combine conditions with & for "and" and | for "or", and wrap each condition in brackets. The Python words and and or raise an error here.
Index Alignment and Missing Values
When two Series are combined, Pandas matches values by label, not by position. Labels that exist in only one Series produce NaN, which stands for a missing value.
s1 = pd.Series([10, 20, 30], index=["a", "b", "c"])
s2 = pd.Series([1, 2, 3], index=["b", "c", "d"])
print(s1 + s2)
# a NaN
# b 21.0
# c 32.0
# d NaN
print(s1.add(s2, fill_value=0))
# a 10.0
# b 21.0
# c 32.0
# d 3.0
The result becomes a float type because NaN is a floating-point value. Alignment is powerful when combining data from different sources, such as sales by city from two months, but it surprises people who expect position-by-position maths.
To deal with gaps, isna() shows where values are missing, fillna() replaces them and dropna() removes them.
Everyday Series Methods and Pitfalls
| Method | What it gives you |
|---|---|
describe() | Count, mean, spread and percentiles in one call |
value_counts() | How often each distinct value appears |
sort_values() | Values in ascending or descending order |
map() | Each value replaced using a dictionary or function |
astype() | A copy converted to another data type |
unique() and nunique() | The distinct values and how many there are |
grades = pd.Series(["A", "B", "A", "C", "A"])
print(grades.value_counts()) # A: 3, B: 1, C: 1
print(grades.map({"A": 4, "B": 3, "C": 2}).mean()) # 3.4
Mistakes to watch for
- Using
apply()with a Python function for simple maths. Vectorised operations such asmarks * 1.1are much faster. - Forgetting that most methods return a new Series.
marks.sort_values()alone does not changemarks; assign the result. - Numbers stored as text. If
dtypeshowsobjectfor a numeric column, convert it withpd.to_numeric()before calculating. - Duplicate labels. An index can repeat, so
s.loc["x"]may return several rows instead of one value. - Confusing a Series with a one-column DataFrame.
df["marks"]returns a Series, whiledf[["marks"]]returns a DataFrame, and some methods behave differently on each.