Data analysis with pandas · Lesson 2 of 15

Series: one column of data

A Series is one column with labels. Create one, pick values by label, do maths on every value at once and count how often each value appears.

About 13 minutes · code runs in your browser, nothing to install

Every column of a DataFrame is a Series: a list of values with a label for each one. You can also make a Series on its own, which is handy for a single list of numbers such as daily sales or temperatures.

Pythoneditable
import pandas as pd

temps = pd.Series([12, 15, 11, 18, 16])
print(temps)
Output
0    12
1    15
2    11
3    18
4    16
dtype: int64

The left column is the index (0 to 4). dtype: int64 means whole numbers; decimals show as float64 and text as object.

Your own labels

Give index= a list of labels and you can read values by name instead of by position, a bit like a dictionary. A dictionary passed to pd.Series works too: its keys become the labels.

Pythoneditable
import pandas as pd

temps = pd.Series([12, 15, 11, 18, 16],
                  index=["Mon", "Tue", "Wed", "Thu", "Fri"])
print(temps["Thu"])
print(temps[["Mon", "Fri"]])

sales = pd.Series({"coffee": 120, "tea": 45, "cake": 30})
print(sales["tea"])
Output
18
Mon    12
Fri    16
dtype: int64
45

One label gives one value; a list of labels gives a smaller Series.

Maths on every value at once

Arithmetic on a Series is done on every value, without a loop. This is called vectorised maths, and it is one reason pandas is fast.

Pythoneditable
import pandas as pd

prices = pd.Series([2.50, 4.00, 1.20], index=["tea", "cake", "milk"])
print(prices * 2)
print((prices * 1.10).round(2))
Output
tea     5.0
cake    8.0
milk    2.4
dtype: float64
tea     2.75
cake    4.40
milk    1.32
dtype: float64

round(2) rounds every value to 2 decimals, here after a 10% price rise.

A comparison also works on every value and gives a Series of True/False. Put that inside square brackets to keep only the values where it is True. You will use this a lot for filtering in Filtering rows with conditions.

Pythoneditable
import pandas as pd

temps = pd.Series([12, 15, 11, 18, 16],
                  index=["Mon", "Tue", "Wed", "Thu", "Fri"])
print(temps > 14)
print(temps[temps > 14])
Output
Mon    False
Tue     True
Wed    False
Thu     True
Fri     True
dtype: bool
Tue    15
Thu    18
Fri    16
dtype: int64

Quick statistics

A Series has methods for the usual summaries. They return NumPy numbers; wrap them in float() or use an f-string to print them cleanly.

Pythoneditable
import pandas as pd

marks = pd.Series([72, 85, 64, 90, 85, 58])
print("Count:", marks.count())
print("Average:", round(float(marks.mean()), 1))
print("Lowest:", marks.min(), "Highest:", marks.max())
print("Median:", float(marks.median()))
Output
Count: 6
Average: 75.7
Lowest: 58 Highest: 90
Median: 78.5

Counting values

value_counts() counts how many times each value appears, most common first. It is the pandas version of the counting dictionary from Dictionaries: look things up by name.

Pythoneditable
import pandas as pd

drinks = pd.Series(["tea", "coffee", "tea", "juice", "coffee", "tea"])
print(drinks.value_counts())
print("Different drinks:", drinks.nunique())
Output
tea       3
coffee    2
juice     1
Name: count, dtype: int64
Different drinks: 3

✎ Exercise

The Series rain holds millimetres of rain for five days. Print the total rain, then print only the days with more than 5 mm.

Hint: Use rain.sum() for the total and rain[rain > 5] to keep the wet days.

Your codewrite your answer, then Run
import pandas as pd

rain = pd.Series([0, 8, 3, 12, 6],
                 index=["Mon", "Tue", "Wed", "Thu", "Fri"])
# your code here

Expected output:

Total: 29 mm
Tue     8
Thu    12
Fri     6
dtype: int64
Show a solution
Solutioneditable
import pandas as pd

rain = pd.Series([0, 8, 3, 12, 6],
                 index=["Mon", "Tue", "Wed", "Thu", "Fri"])
print("Total:", rain.sum(), "mm")
print(rain[rain > 5])
Output
Total: 29 mm
Tue     8
Thu    12
Fri     6
dtype: int64

Check yourself

1. What does s * 2 do to a Series s?

2. What does s[s > 10] return?

3. Which method counts how often each value appears?

Spotted a mistake on this page? Tell us