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.
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.
import pandas as pd
temps = pd.Series([12, 15, 11, 18, 16])
print(temps)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.
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"])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.
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))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.
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])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.
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()))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.
import pandas as pd
drinks = pd.Series(["tea", "coffee", "tea", "juice", "coffee", "tea"])
print(drinks.value_counts())
print("Different drinks:", drinks.nunique())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.
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
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])Total: 29 mm Tue 8 Thu 12 Fri 6 dtype: int64
Check yourself
1. What does s * 2 do to a Series s?
Arithmetic on a Series is applied to each value: every number is doubled.
2. What does s[s > 10] return?
s > 10 makes a True/False Series; using it inside brackets keeps the values where it is True.
3. Which method counts how often each value appears?
count() counts non-missing values, nunique() counts distinct values, and value_counts() gives a count per value.