[Python] Conditional Sum in Pandas based on current row value

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

  1. Stack

    Stack Membro Participativo

    I'm new to Python and pandas, and I need some help with a problem I'm facing.

    I have the following table:

    employeeID groupName agreementID agreementDate vesting_value_CAD expiryDate
    2 A 8 2/1/2025 50 3/31/2025
    2 A 7 3/1/2025 200
    2 A 6 3/1/2025 40
    2 A 1 1/1/2025 500 1/31/2025
    2 B 9 3/1/2025 30

    I need to sum the vesting_value_CAD for each employee, group, and agreement date, where the expiryDate of other rows is greater than or equal to the current row's agreementDate. For example, for the first row (employeeID 2 in group A with agreementDate 2/1/2025), the sum of vesting_value_CAD should be 200 + 40, that is the sum of agreement IDs 7 and 6.

    Could someone please guide me on how to achieve this without using a function or a for loop?

    Thank you in advance for your help!

    Continue reading...

Compartilhe esta Página