BBampaa

Lessons › Python

Python lesson

Counting values

Lagos appears 5 times and Abuja 3. nunique() gives just the number of different values: 2 stores.

Try this lesson free →
About 5–10 minutes · no sign-up needed

Example

value_counts() counts how often each value appears in a column:

df["Store"].value_counts()

Output:

Lagos 5
Abuja 3

Key points

.value_counts()How many times each value appears, biggest first.
.nunique()How many different values there are.
.unique()The list of different values.

Put together: df["Product"].value_counts() shows how many orders each product had.

Practice

Try each one in your head first, then open the answer.

1. Build a line that counts the orders for each product.
df["Product"].value_counts()
2. Build a line that shows how many different stores there are.
df["Store"].nunique()
3. Count how many orders each store had.
df["Store"].value_counts()
4. Show how many different days appear.
df["Day"].nunique()
5. Count how many orders each product had.
df["Product"].value_counts()

Learn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.

Try this lesson free →
About 5–10 minutes · no sign-up needed