Pandas · Count rows and unique values
Google Search Analytics
Course overviewGoogle Search · Summarize search activity

Count rows and unique values

Distinguish all rows, recorded values, distinct users, and frequency counts.

Step 1 of 3 · Learn

Choose what you count

len(df) counts rows. A column's .count() counts non-missing values, including repeats. .nunique() counts distinct non-missing values by default. Missing user IDs do not identify distinct people.

indexuser_id
07
17
29
3NA
indexrowsrecordedunique
0432
Four rows, three recorded IDs, two distinct known IDs.

Count each value

.value_counts(dropna=False) includes a missing-value bucket. It returns a Series; .rename_axis("label").reset_index(name="rows") turns its index into a column. Sort explicitly when you need a fixed output order.

indexlabel
0A
1B
2A
indexlabelrows
0A2
1B1
Frequency counts retain repeated observations.

Build a one-row report

pd.DataFrame({"metric": [value]}) creates a one-row DataFrame. Put each scalar inside a list, and add dictionary entries in the required column order.

▷ Your turn

Return one row with columns searches and known_user_observations. Count every row of searches, then non-missing user_id values, including repeats. Save the DataFrame as result.

LANGUAGEPython · Pandas

Loading Python and Pandas…

Run the code to see DataFrame results here.