Sub topic 1
What you are counting: Series and DataFrame columns
Every counting method in this tutorial runs on a pandas object. The method's result depends on whether you call it on a Series or a DataFrame, so this chapter covers that distinction first.
Prerequisites
This chapter assumes you can:
- Write basic Python: variables, lists, dictionaries, and method calls.
- Create a pandas Series and DataFrame.
- Select a column from a DataFrame with square brackets.
The two objects
A Series is a one-dimensional labeled array with a single data type. A DataFrame is a table of Series that share the same row index. Most counting methods in this tutorial work on one Series at a time, usually a single column.
The examples in this tutorial use this small table. One city is missing.
import pandas as pd
df = pd.DataFrame({
"city": ["Oslo", "Rome", "Oslo", "Paris", "Rome", "Oslo", None],
"plan": ["free", "pro", "free", "pro", "pro", "pro", "free"],
})
print(df.shape) # (7, 2): 7 rows, 2 columns
print(type(df["city"])) # <class 'pandas.core.series.Series'>
print(type(df[["city"]])) # <class 'pandas.core.frame.DataFrame'>The bracket style decides which object you get. df["city"] returns a Series. df[["city"]], with a list inside the brackets, returns a one-column DataFrame. Methods such as value_counts and unique exist on both objects, but they behave differently. On a Series they count values. On a DataFrame they count whole rows. A common mistake is to pass a double-bracketed selection and then wonder why the result has a MultiIndex.
Replace that placeholder with the diagram below.
Length is not the count of values
A Series has a length and a count of non-missing values. These can differ, and later chapters depend on the difference.
city = df["city"]
print(len(city)) # 7: every row, including the missing one
print(city.count()) # 6: only the non-missing entries
print(city.isna().sum()) # 1: the number of missing entries
print(city.dtype) # object: strings are stored as Python objectsThe missing entry was written as None. pandas stores it as a missing value, which prints as None or NaN depending on the data type. Whether a counting method includes it is a setting you choose per call, and the next chapters cover each case. As a rule, len counts rows, while count skips missing values.
Rows, columns, and what each method returns
Keep these results in mind for the rest of the tutorial:
| Object | Method call | What it counts |
|---|---|---|
| Series | `city.value_counts()` | Each distinct value |
| Series | `city.unique()` | Distinct values, no counts |
| DataFrame | `df.duplicated()` | Rows that repeat an earlier row |
| DataFrame | `df.value_counts()` | Distinct rows |
Most results are a new Series or array. They do not change df.
Exercise
Given the DataFrame above, what are len(df["plan"]) and df["city"].count(), and why do they differ?
Show answer
len(df["plan"]) is 7 because len counts every row. df["city"].count() is 6 because count skips the one missing city value.