Skip to main content

pandas Series ๐Ÿ“ˆ

Mentor's Note: A Series is the simplest building block in pandas. Before you can understand a DataFrame (a full table), you need to understand a Series (a single column). Think of it as a Python list that knows the name of every item. ๐Ÿ’ก

What You'll Learn

By the end of this tutorial, you'll know:

  • What a pandas Series is and how it differs from a Python list
  • How to create a Series from a list (default index) and from a dictionary (custom index)
  • How to filter, slice, and do math on a Series without writing any loops
  • The common mistake of assuming s[0] always works when you have a custom index

๐ŸŒŸ The Scenario: The Marks Register Columnโ€‹

Your teacher has a register book. The left column has student names, the right column has marks. If you tear out just the marks column and keep the names as labels โ€” that's a pandas Series.

  • The Logic:
    • Values: [95, 82, 70] โ€” the actual data
    • Index: ['Vishnu', 'Ankit', 'Priya'] โ€” the labels
    • Name: 'marks' โ€” what this column represents
  • The Result: A 1D array where every value has a meaningful label. โœ…

๐Ÿ“– Concept Explanationโ€‹

1. What is a Series?โ€‹

A pandas Series is a one-dimensional labelled array. It has:

  • Values โ€” the data (numbers, strings, booleans, etc.)
  • Index โ€” labels for each value (default: 0, 1, 2... or custom strings)
  • dtype โ€” the data type of the values (int64, float64, object, etc.)
  • name โ€” an optional name for the Series

2. Default Index vs Custom Indexโ€‹

Default IndexCustom Index
HowCreated from a listCreated from a dictionary
Labels0, 1, 2, ...'Vishnu', 'Ankit', ...
Accesss[0]s['Vishnu']

3. Series vs Python Listโ€‹

FeaturePython Listpandas Series
LabelsโŒ Index onlyโœ… Custom labels
Vectorised mathโŒ Needs loopโœ… s * 2, s + 10
Built-in statsโŒ Needs codeโœ… .mean(), .max()
Filter by conditionVerboses[s > 80]

๐ŸŽจ Visual Logicโ€‹


๐Ÿ’ป Implementationโ€‹

import pandas as pd

# Create a Series from a list
marks = pd.Series([95, 82, 70])

print(marks)
# 0 95
# 1 82
# 2 70
# dtype: int64

print(marks.index) # RangeIndex(start=0, stop=3, step=1)
print(marks.values) # [95 82 70]
print(marks.dtype) # int64

๐Ÿ“Š Sample Dry Runโ€‹

Series: marks = pd.Series({'Vishnu': 95, 'Ankit': 82, 'Priya': 70, 'Sara': 88})

Expression: marks[marks > 80]

StepActionResult
1Evaluate marks > 80Vishnu: True, Ankit: True, Priya: False, Sara: True
2Use boolean mask to filterKeep rows where mask is True
3Return filtered SeriesVishnu: 95, Ankit: 82, Sara: 88

๐ŸŽฏ Practice Lab ๐Ÿงชโ€‹

Task: City Population Analysis

Create a pandas Series of 5 Indian city populations (in millions):

  • Surat: 7.8, Mumbai: 20.7, Delhi: 31.2, Chennai: 10.9, Kolkata: 14.9

Then answer these questions using Series methods:

  1. Which city has the highest population? (idxmax())
  2. What is the average population? (.mean())
  3. Filter cities with population above 12 million.
  4. Add 0.5 million to every city (vectorised addition).

Hint: Pass a dictionary to pd.Series() with city names as keys.


โ“ Frequently Asked Questionsโ€‹

Q: I created a Series with string labels. Why does s[0] give a KeyError?

When you create a Series from a dictionary, the keys become the index. If your index is ['Vishnu', 'Ankit', 'Priya'], then s[0] looks for a label 0 โ€” which doesn't exist. Use s.iloc[0] to access by position, or s['Vishnu'] to access by label. This is one of the most common beginner mistakes.

Q: What's the difference between label-based slicing and position-based slicing?

With a custom string index, s['Ankit':'Sara'] includes both endpoints (inclusive). With integer position slicing s[0:2], the end is exclusive (like a Python list). This inconsistency trips up many beginners โ€” use .loc[] for label-based and .iloc[] for position-based access to be explicit.

Q: Can a Series hold mixed data types?

Yes, but the dtype will become object (like a Python list). Performance and memory use suffer because pandas can't use optimised numeric operations. Try to keep Series columns uniform โ€” all integers, all floats, or all strings.

Q: CBSE exam โ€” how do you create a Series from a dictionary?

s = pd.Series({'key1': val1, 'key2': val2}) โ€” dictionary keys become the index labels. This is the standard CBSE answer.


โœ… Summaryโ€‹

In this tutorial, you've learned:

  • โœ… A pandas Series is a 1D labelled array โ€” values plus an index
  • โœ… Create from a list (default 0-based index) or a dictionary (keys become labels)
  • โœ… Vectorised operations like s + 5 or s * 1.1 work on every element โ€” no loop needed
  • โœ… Boolean filtering s[s > 80] returns a new Series with only matching values
  • โœ… s[0] fails with a custom string index โ€” use s.iloc[0] for position, s['label'] for labels

๐Ÿ’ก Interview & Exam Tipsโ€‹

Q: What is the default index of a Series created from a list?

Integer-based RangeIndex starting from 0: 0, 1, 2, ...

Q: What is the difference between a Python list and a pandas Series?

A Series has a labelled index, supports vectorised math (s * 2), and has built-in statistical methods (.mean(), .max()). A list has none of these.

Q: How do you create a Series with custom labels?

Pass a dictionary: pd.Series({'Vishnu': 95, 'Ankit': 82}) โ€” keys become the index labels.

Q: What does s[s > 80] return?

A new Series containing only the values where the condition is True โ€” this is called boolean indexing.


๐Ÿ“š Further Readingโ€‹

Continue your learning path:

Go deeper: