[Python] How to count the number of times a value has appeared so far in a Python pandas...

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

  1. Stack

    Stack Membro Participativo

    I have two datasets from an animal shelter that include Animal IDs and dates. One set is for intakes, one is for outcomes. Some Animal IDs are duplicated, typically because the same animal has entered/exited the shelter multiple times. I want to add a column indicating how many times that animal has entered/exited so far (the dataframes are organized by date). How would I do this?

    Example intake data:

    Animal ID Intake Date
    1 12/07/2017 02:07:00 PM
    2 12/08/2017 01:10:00 PM
    1 01/06/2018 12:03:00 PM
    3 01/07/2018 01:10:00 PM
    2 01/08/2018 04:15:00 PM

    What I want is to add this column to each dataframe:

    Animal ID Intake Date Shelter Visits
    1 12/07/2017 02:07:00 PM 1
    2 12/08/2017 01:10:00 PM 1
    1 01/06/2018 12:03:00 PM 2
    3 01/07/2018 01:10:00 PM 1
    2 01/08/2018 04:15:00 PM 2

    This would be to clarify join conditions when I want to combine the datasets (match each Animal ID not only with itself, but with what visit it represents). How do I do this?

    Continue reading...

Compartilhe esta Página