Learning Objectives – after this chapter you will be able to:
  • Create a Series from a list, a dictionary and a scalar value.
  • Use custom index labels and access values with [], loc and iloc.
  • Perform vectorised operations and filtering on a Series.
  • Use common Series methods for statistics and missing values.
What is a Series?

A Series is a one-dimensional labelled array. It has values and an index (labels). If you do not give labels, pandas uses 0, 1, 2 ….

pd.Series(data, index=labels, name=...)
From a list
import pandas as pd

s = pd.Series([10, 20, 30, 40])
print(s)
print(s[2])
print(s.index.tolist(), s.values.tolist())
Output
0    10
1    20
2    30
3    40
dtype: int64
30
[0, 1, 2, 3] [10, 20, 30, 40]
With custom index labels
import pandas as pd

temps = pd.Series([31.5, 29.0, 33.2], index=["Mon", "Tue", "Wed"], name="Temp")
print(temps)
print(temps["Tue"])
Output
Mon    31.5
Tue    29.0
Wed    33.2
Name: Temp, dtype: float64
29.0
From a dictionary and from a scalar
import pandas as pd

seats = {"BCA": 60, "BBA": 40, "MCA": 30}       # sample data
s = pd.Series(seats)
print(s)
print(pd.Series(seats, index=["MCA", "BCA"]))      # choose labels
print(pd.Series(5, index=["a", "b", "c"]).tolist())           # scalar repeated
Output
BCA    60
BBA    40
MCA    30
dtype: int64
MCA    30
BCA    60
dtype: int64
[5, 5, 5]
Accessing Values: loc and iloc
AccessorSelects bySlice end
s.loc[...]Index labelIncluded
s.iloc[...]Integer position (0, 1, 2 …)Excluded (like Python)
import pandas as pd

marks = pd.Series([82, 67, 91, 74], index=["Asha", "Ravi", "Meera", "John"])
print(marks.loc["Meera"], marks.iloc[0], marks.iloc[-1])
print(marks.loc["Ravi":"John"].tolist())     # label slice: end included
print(marks.iloc[1:3].tolist())              # position slice: end excluded
print(marks.loc[["Asha", "John"]].tolist())
Output
91 82 74
[67, 91, 74]
[67, 91]
[82, 74]
Operations and Filtering

Operations apply to every value at once (vectorised). When two Series are combined, values are aligned by index label; labels found in only one Series give NaN (missing).

import pandas as pd

marks = pd.Series([82, 67, 91, 74], index=["Asha", "Ravi", "Meera", "John"])
print((marks + 5).tolist())
print(marks[marks >= 75])
print((marks >= 75).sum(), "students scored 75 or more")

a = pd.Series([1, 2, 3], index=["x", "y", "z"])
b = pd.Series([10, 20, 30], index=["y", "z", "w"])
print(a + b)
Output
[87, 72, 96, 79]
Asha     82
Meera    91
dtype: int64
2 students scored 75 or more
w     NaN
x     NaN
y    12.0
z    23.0
dtype: float64
Useful Series Methods
import pandas as pd

sales = pd.Series([120, 340, 90, 340, 210])
print(sales.sum(), sales.mean(), sales.max(), sales.min())
print(sales.median(), sales.idxmax())
print(sales.sort_values(ascending=False).tolist())
print(sales.value_counts().loc[340])     # how many times 340 occurs
print(sales.nunique(), sales.size)
Output
1100 220.0 340 90
210.0 1
[340, 340, 210, 120, 90]
2
4 5
Missing values (NaN)
import pandas as pd
import numpy as np

s = pd.Series([4.0, np.nan, 7.0, np.nan])
print(s.isna().tolist())
print(s.count(), s.size)        # count() ignores missing values
print(s.fillna(0).tolist())
print(s.dropna().tolist())
print(s.mean())                 # NaN skipped automatically
Output
[False, True, False, True]
2 4
[4.0, 0.0, 7.0, 0.0]
[4.0, 7.0]
5.5
Key Takeaways
  • A Series = values + index labels; it can be built from a list, dict or scalar.
  • loc uses labels (slice end included); iloc uses positions (end excluded).
  • Operations are vectorised and aligned by label; mismatched labels give NaN.
Video : Pandas Series
Question / Answer (15 questions)

Answer : A Series is a one-dimensional labelled array that holds data of any type. It consists of values and an index of labels for those values.

Answer : A RangeIndex 0, 1, 2, … up to n − 1, where n is the number of values.

Answer : Pass the index argument: pd.Series([31.5, 29.0, 33.2], index=["Mon", "Tue", "Wed"]).

Answer : The dictionary keys become the index labels and the values become the Series values. If an index list is also given, only those keys are used, in that order.

Answer : A Series with the scalar value 5 repeated for each label: a 5, b 5, c 5.

Answer : name gives the Series a title. It is displayed when the Series is printed and becomes the column name if the Series is added to a DataFrame.

Answer : loc selects by index label and includes the end label in slices. iloc selects by integer position and excludes the end position in slices, like Python lists.

Answer : The values for Ravi, Meera and John – the end label is included in label-based slicing.

Answer : s.iloc[0] for the first value and s.iloc[-1] for the last.

Answer : Use a boolean condition as an index: marks[marks >= 75] returns only the values that satisfy the condition, with their labels.

Answer : When two Series are combined, pandas matches values by index label, not by position. Labels present in only one Series produce NaN in the result.

Answer : NaN (Not a Number) marks a missing value. Methods such as mean(), sum() and count() skip NaN values by default.

Answer : isna() detects missing values, fillna(value) replaces them, and dropna() removes them.

Answer : A Series that counts how many times each distinct value occurs, sorted by count in descending order by default.

Answer : count() returns the number of non-missing values, while size returns the total number of values including NaN.
MCQ Test : Check Your Understanding (5 questions)

Select an option and press Check Answer, or answer all questions and press Submit Test to see your score. Each answer comes with an explanation.

Q1. pd.Series([5, 6, 7]).iloc[1] returns:

Show Answer

Correct Answer : B. 6
iloc[1] is the second value.

Q2. Which accessor uses LABELS and includes the slice end?

Show Answer

Correct Answer : D. loc
loc is label-based and includes the end label.

Q3. When a Series is created from a dictionary, the keys become the:

Show Answer

Correct Answer : A. Index labels
Keys form the index; dictionary values form the data.

Q4. pd.Series([1, None, 3]).count() returns:

Show Answer

Correct Answer : C. 2
count() ignores missing values.

Q5. Adding two Series with partly different labels gives NaN for:

Show Answer

Correct Answer : B. Labels present in only one Series
Values are aligned by label; unmatched labels have no partner.

Bibliography/References

The notes, questions and MCQs on this page are original explanations written for this course. The works listed below were consulted and are cited for attribution and further reading. Product names, logos and trademarks belong to their respective owners. Short quotations, where used, are marked and attributed.

  1. pandas development team. (n.d.). Intro to data structures. pandas User Guide. https://pandas.pydata.org/docs/user_guide/dsintro.html
  2. pandas development team. (n.d.). Indexing and selecting data. pandas User Guide. https://pandas.pydata.org/docs/user_guide/indexing.html
  3. pandas development team. (n.d.). Working with missing data. pandas User Guide. https://pandas.pydata.org/docs/user_guide/missing_data.html
  4. pandas development team. (n.d.). 10 minutes to pandas. pandas User Guide. https://pandas.pydata.org/docs/user_guide/10min.html
  5. McKinney, W. (2022). Python for Data Analysis (3rd ed.). O’Reilly Media. (Open-access online edition.) https://wesmckinney.com/book/