Series: One Column of Values
A Series is pandas' column, one list of values with a label for each value (its index) and one dtype, and math such as price * 1.07 or sum, mean, max and round works on every value at once, with no loop.
Apprentice · Python · Updated
After this lesson you can
- Make a Series with pd.Series, and read its values by label, its index and its dtype
- Do math on a whole Series at once, and summarize it with sum, mean, max, min and round
Key takeaways
- A Series is one column of values. Each value has a label, together called the index, and every value shares one dtype, the kind of value it holds.
- Without labels of your own, the index counts from 0. With index=, each value gets a name, such as a product, and series["honey jar"] reads it.
- Math on a Series works on every value at once, so prices * 1.07 adds tax to the whole column and each value keeps its label.
- sum(), mean(), max() and min() summarize a Series in one call, and round(2) cleans up the tiny leftovers a computer's decimals carry.
A price list, a week of temperatures, the scores on a quiz: a lot of data is one column of values. In pandas, every column you take out of a table is a Series, so you'll work with them all the time: adding tax to every price, averaging a column of scores, finding the biggest sale of the season.
The examples come from Sunnyside Farmers Market, a Saturday market whose vendors sell produce, bread, flowers, honey and crafts. The market and its vendors are our own made-up data.
One column of values
Green Acre Greens sold 21, 25, 18, 30 and 27 heads of lettuce on the season's first five Saturdays. In plain Python that's a list; pd.Series makes it a Series, pandas' column:
import pandas as pd
lettuce = pd.Series([21, 25, 18, 30, 27])
lettuce
0 21
1 25
2 18
3 30
4 27
dtype: int64
Each value now has a label, on the left. With no labels of your own, pandas counts from 0, the way list positions do. The last line is the Series' dtype, short for "data type": the kind of value every row holds. int64 means whole numbers. Every value in a Series shares one dtype, which is what lets pandas do math on all of them at once. Text gets its own:
categories = pd.Series(["produce", "honey", "baked goods"])
categories
0 produce
1 honey
2 baked goods
dtype: str
In pandas 3, text shows as str. Older tutorials and answers online show object for the same thing. A number with a decimal point is float64.
Labels: the index
Numbers from 0 are fine for a list of Saturdays, but a price list reads better with names. Pass them as index=, one label for each value:
prices = pd.Series([3.00, 12.00, 8.00, 18.00, 35.00],
index=["lettuce", "honey jar", "sourdough loaf", "mixed bouquet", "cutting board"])
prices
lettuce 3.0
honey jar 12.0
sourdough loaf 8.0
mixed bouquet 18.0
cutting board 35.0
dtype: float64
The labels together are the Series' index: the way you find a value, like the name on a coat-check ticket. Square brackets read a value by its label, and .index shows all the labels:
print(prices["honey jar"])
print(prices.index)
12.0
Index(['lettuce', 'honey jar', 'sourdough loaf', 'mixed bouquet',
'cutting board'],
dtype='str')
A label that isn't there, such as prices["tomatoes"], stops with KeyError: 'tomatoes', the same error a dictionary gives.
Math on the whole column at once
The county adds a 7% sales tax, so each price times 1.07 is what a shopper pays. With a list, you'd loop over the prices. With a Series, multiply the Series:
with_tax = prices * 1.07
with_tax
lettuce 3.21
honey jar 12.84
sourdough loaf 8.56
mixed bouquet 19.26
cutting board 37.45
dtype: float64
Every value was multiplied, and each kept its label. The same works with +, - and /, and with two Series of the same length, which is how a table's units * price gives every row's total in one step.
Summaries in one call
A Series can summarize itself. mean() is the average: add the values up, divide by how many there are.
print("total:", prices.sum())
print("average:", prices.mean())
print("highest:", prices.max())
print("lowest:", prices.min())
total: 76.0
average: 15.2
highest: 35.0
lowest: 3.0
Add up the prices with tax, though, and something odd shows:
print("total with tax:", with_tax.sum())
print("rounded:", with_tax.sum().round(2))
total with tax: 81.32000000000001
rounded: 81.32
Computers store most decimals as very close approximations, so a long sum can carry a tiny leftover. pandas hid it when it showed with_tax with two decimals, but it was there. round(2) rounds to two decimal places, cents for money, and works on a single number or on a whole Series (with_tax.round(2)).
Check yourself
2 questions to try on the lesson page.
So what
A Series is one labeled column that does its own math, so a whole column's tax, total or average takes one line, the same line for five rows or five million.