The Pandas Series
A Series is a single labelled column of data — like one column from a spreadsheet.
What you will learn
- Create a Series
- Use labels (the index) to look up values
- See how a Series differs from a list
One labelled column
Pandas (a free Python library for working with tables of data — think of it as a programmable spreadsheet) is the most important tool in data science — it is how you load, clean and explore data. Its two building blocks are the Series (one column) and the DataFrame (a whole table). We start with the Series.
A Series is a list of values plus a label for each value. Those labels are called the index. Think of one column in a spreadsheet, where each cell also has a name.
import pandas as pd
sales = pd.Series([250, 300, 175],
index=['Mon', 'Tue', 'Wed'])
print(sales)Note: Output: Mon 250 Tue 300 Wed 175 dtype: int64 Each number has a label on its left. dtype: int64 means the values are whole numbers.
Look things up by their label
Because each value has a label, you can grab it by name — much friendlier than remembering a position number.
print('Tuesday:', sales['Tue']) # look up by label
print('average:', sales.mean()) # a Series has the maths functions too
print('best :', sales.max())Note: Output: Tuesday: 300 average: 241.66666666666666 best : 300 We asked for Tuesday (300), the average of the three days (about 241.7), and the highest day (300, which was Tuesday). A Series keeps all the array maths.
Keep only the values you want
Just like a NumPy array, you can give a Series a True/False condition in the brackets and it keeps only the rows where the condition is True. This is the exact trick you will use to filter whole tables later.
print(sales[sales > 200]) # keep only days that sold over 200Note: Output: Mon 250 Tue 300 dtype: int64 Wednesday (175) was below 200, so it was dropped. The labels travel with their values, so we still see which days survived.
| Python list | Pandas Series | |
|---|---|---|
| Access by | Position only (x[0]) | Label or position |
| Has labels? | No | Yes (the index) |
| Built-in mean/max? | No | Yes |
| It is really | A simple sequence | A NumPy array + labels |
Tip: A Series is basically a NumPy array with labels bolted on — so it keeps all the fast maths (.mean(), .max()) and adds friendly look-up by name.
Q. What is the “index” of a Pandas Series?
✍️ Practice
- Create a Series of three friends’ ages with their names as the index.
- Print one friend’s age by their name, then print the average age.
🏠 Homework
- Make a Series of your weekly spending (one value per day, days as the index). Print the total for the week and your biggest spending day.