[Python] Obtaining diff for multiple columns in pandas

Discussão em 'Python' iniciado por Stack, Janeiro 31, 2025.

  1. Stack

    Stack Membro Participativo

    i have a pandas dataframe with multiple columns (on which I am computing the cumulative value).

    I would like to get the incremental value for the same now.

    This is my current dataset:

    Gender Cubic_Cap Branch UWYear yhat_all
    M 1000 A 2015 19
    M 1000 A 2015 20
    M 1000 A 2015 26
    M 1000 A 2015 30
    F 1500 B 2016 1
    F 1500 B 2016 25
    F 1500 B 2016 36
    F 1500 B 2016 49


    My desired result is:

    yhat_incremental
    0
    1
    6
    4
    1
    24
    11
    13


    I've tried the following methods (but to no avail):

    all_c1['incremental_yhat'] = all_c1.groupby(rating_factors)['yhat_all'].diff().fillna(all_c1['yhat_all'])


    I've also tried this:

    all_c1['incremental_yhat'] = all_c1['yhat_all'].shift().where(all_c1[rating_factors].eq(all_c1[rating_factors].shift()))


    Is there any other methods I can use to obtain this?

    Continue reading...

Compartilhe esta Página