[Python] Calculate difference between rows in Pandas dataframe using conditional logic...

Discussão em 'Python' iniciado por Stack, Novembro 27, 2024.

  1. Stack

    Stack Membro Participativo

    I am trying to use the pandas.DataFrame.diff function to calculate the difference between rows in a dataframe. The catch is I only want to calculate the difference for certain values using some simple conditional logic applied to the value in the 'data' column (i.e. if 'data' column value is 0 then set the difference to 0. Note it doesn't have to be set to 0 it could also be set as NaN).

    Sample code is provided below and the table shows current results and the last column with my desired result. The 'diff_b' column almost does what I want but from outputs in the table you can see it's quite right.

    import pandas as pd

    # Sample DataFrame
    df = pd.DataFrame({'data': [10, 20, 0, 25, 0, 0, 30, 25, 40]})

    df['diff'] = df.data.diff()


    Table showing results and desired result in last column

    value | diff | desired_result
    10 NaN NaN
    20 10 10
    0 -20 0
    25 25 25
    0 -25 0
    0 0 0
    30 30 30
    25 -5 -5
    40 15 15


    I have tried getting the following code with conditional logic however this does not work

    df['diff'] = df['data'].apply(lambda x: 0 if x == 0 else df.data.diff())

    Continue reading...

Compartilhe esta Página