[Python] Create a cumulative count column in pandas dataframe

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

  1. Stack

    Stack Membro Participativo

    I have a dataframe set up similar to this

    **Person Value**
    Joe 3
    Jake 4
    Patrick 2
    Stacey 1
    Joe 5
    Stacey 6
    Lara 7
    Joe 2
    Stacey 1


    I need to create a new column 'x' which keeps a running count of how many times each person's name has appeared so far in the list.

    Expected output:

    **Person Value** **x**
    Joe 3 1
    Jake 4 1
    Patrick 2 1
    Stacey 1 1
    Joe 5 2
    Stacey 6 2
    Lara 7 1
    Joe 2 3
    Stacey 1 3


    All I've managed so far is to create an overall count, which is not what I'm looking for.

    Continue reading...

Compartilhe esta Página