[Python] Why does `groupby` return fewer rows than the number of unique rows after removing...

Discussão em 'Python' iniciado por Stack, Abril 26, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working with a DataFrame df and trying to understand why the result of a groupby operation has fewer rows than expected. Here's what I did:

    columnas = ["description", "surface_total", "l3", "rooms"]

    # Count duplicated rows based on a subset of columns
    df.duplicated(subset=columnas).sum()
    # Output: 25317

    # Count unique rows based on those columns
    df.shape[0] - df[columnas].duplicated().sum()
    # Output: 72915

    # Check for missing values in the column I'm aggregating
    df.price.isna().sum()
    # Output: 0

    # Group by the selected columns and compute the mean price
    df.groupby(by=columnas).price.mean().shape
    # Output: (48145,)


    Since there are no missing values in price, and after removing duplicates I have 72,915 unique rows based on columnas, I expected the grouped result to also have 72,915 rows. However, groupby returns only 48,145 rows.

    Why does groupby produce fewer rows than the number of unique rows, even when there are no missing values in the aggregated column?

    Continue reading...

Compartilhe esta Página