Chapter 3: Pandas Series
What is a Pandas Series, really?
A Series is:
- A one-dimensional labeled array
- It has data values + an index (labels for each value)
- You can think of it as one column from a spreadsheet — but with superpowers
It is very similar to:
- A Python list or NumPy array … but with labels
- A single column in Excel or Google Sheets
- A dictionary where keys are ordered and can be any type (not just strings)
1. Creating a Series – the most common ways
|
0 1 2 3 4 5 6 |
import pandas as pd |
Way 1 – From a Python list (most beginner-friendly)
|
0 1 2 3 4 5 6 7 8 |
temperatures = pd.Series([24.5, 25.8, 23.1, 26.4, 22.9]) print(temperatures) |
Output:
|
0 1 2 3 4 5 6 7 8 9 10 11 |
0 24.5 1 25.8 2 23.1 3 26.4 4 22.9 dtype: float64 |
→ By default, pandas gives it a numeric index starting from 0.
Way 2 – With meaningful index (this is very common)
|
0 1 2 3 4 5 6 7 8 9 10 11 |
temperatures = pd.Series( [24.5, 25.8, 23.1, 26.4, 22.9], index=['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'] ) print(temperatures) |
Output:
|
0 1 2 3 4 5 6 7 8 9 10 11 |
Monday 24.5 Tuesday 25.8 Wednesday 23.1 Thursday 26.4 Friday 22.9 dtype: float64 |
→ Now we can use day names to access values — much more readable!
Way 3 – From a Python dictionary (very natural)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
sales = pd.Series({ 'Apple': 450, 'Banana': 320, 'Orange': 180, 'Mango': 290, 'Grapes': 150 }) print(sales) |
Output:
|
0 1 2 3 4 5 6 7 8 9 10 11 |
Apple 450 Banana 320 Orange 180 Mango 290 Grapes 150 dtype: int64 |
→ Dictionary keys become the index, values become the data — very clean.
Way 4 – From scalar value (broadcasting — rare but useful)
|
0 1 2 3 4 5 6 7 8 |
bonus = pd.Series(5000, index=['Priya', 'Rahul', 'Sneha', 'Vikram']) print(bonus) |
|
0 1 2 3 4 5 6 7 8 9 10 |
Priya 5000 Rahul 5000 Sneha 5000 Vikram 5000 dtype: int64 |
2. How to access values in a Series (very important)
There are two main ways — and you must know both.
| Goal | Syntax | When to use |
|---|---|---|
| Access by position (0,1,2…) | s.iloc[2] | When order matters |
| Access by label (index) | s[‘Tuesday’] or s.loc[‘Tuesday’] | When labels are meaningful |
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
days_temp = pd.Series( [24.5, 25.8, 23.1, 26.4, 22.9], index=['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] ) print(days_temp['Tue']) # 25.8 print(days_temp.loc['Tue']) # same as above print(days_temp.iloc[1]) # 25.8 (position 1) # Multiple values print(days_temp[['Mon', 'Wed', 'Fri']]) print(days_temp.iloc[[0, 2, 4]]) |
3. Important properties & methods you use all the time
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
s = pd.Series([78, 92, 65, 88, 71, 59], index=['Aarav','Diya','Rohan','Isha','Vihaan','Kian']) print(s.index) # Index(['Aarav', 'Diya', 'Rohan', 'Isha', 'Vihaan', 'Kian'], dtype='object') print(s.values) # array([78, 92, 65, 88, 71, 59]) print(s.dtype) # int64 print(s.shape) # (6,) print(s.size) # 6 print(s.name) # None (you can set it: s.name = 'marks') # Most useful summary methods print(s.mean()) # 75.5 print(s.median()) # 74.5 print(s.min()) # 59 print(s.max()) # 92 print(s.sum()) # 453 print(s.describe()) # count, mean, std, min, 25%, 50%, 75%, max |
4. Filtering & conditions (this is where Series starts to shine)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
marks = pd.Series({ 'Aarav': 78, 'Diya': 92, 'Rohan': 65, 'Isha': 88, 'Vihaan': 71, 'Kian': 59 }) # Boolean Series (very powerful) print(marks >= 80) # Output: Aarav False Diya True Rohan False Isha True Vihaan False Kian False dtype: bool # Use it to filter print(marks[marks >= 80]) # Output: Diya 92 Isha 88 dtype: int64 # Multiple conditions print(marks[(marks >= 70) & (marks <= 85)]) |
5. Creating new Series from calculations
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# Add bonus to everyone bonus_marks = marks + 5 print(bonus_marks) # Percentage percent = marks / 100 * 100 print(percent.round(1)) # Conditional new series result = pd.Series('Fail', index=marks.index) result[marks >= 70] = 'Pass' print(result) |
6. Very common real-life Series examples
Example 1 – Daily sales
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
daily_sales = pd.Series( [3400, 2800, 4100, 3700, 2900], index=pd.date_range('2025-02-01', periods=5) ) print(daily_sales.mean()) # average daily sales print(daily_sales.idxmax()) # day with highest sales print(daily_sales.cumsum()) # running total |
Example 2 – Employee ratings
|
0 1 2 3 4 5 6 7 8 9 10 11 12 |
ratings = pd.Series( [4.1, 3.8, 4.6, 4.9, 3.2, 4.0], index=['Priya','Rahul','Aniket','Sneha','Vikram','Meera'] ) top_performers = ratings[ratings >= 4.5].sort_values(ascending=False) print(top_performers) |
Summary – Series in one table (keep this close)
| Concept | Example code | What you get |
|---|---|---|
| Create from list | pd.Series([1,2,3,4]) | Series with 0,1,2,3 index |
| Create with index | pd.Series([10,20], index=[‘A’,’B’]) | Labeled Series |
| Access by label | s[‘Mon’] or s.loc[‘Mon’] | Single value |
| Access by position | s.iloc[0] | Single value |
| Filter | s[s > 50] | New filtered Series |
| Basic stats | s.mean(), s.median(), s.describe() | Numbers |
| Boolean condition | s >= 80 | Series of True/False |
| Name the Series | s.name = ‘scores’ | Helps when it becomes a column |
Now — what would you like to do next with Series?
- Practice indexing & slicing in more detail
- See how Series lives inside DataFrames (very important next step)
- Do more filtering & conditions examples
- Work with missing values (NaN) in Series
- Try time series with dates as index
- Convert Series → DataFrame and back
Just tell me which direction feels most useful or interesting right now. I’ll continue explaining slowly with real examples. 😊
