Search code examples
pythonpandasdataframedelta

How can I calculate delta at dataframe?


A   B   C   Delta
**1 Jan 10  0**
1   Feb 20  10
1   Mar 40  30
**2 Jan 10  0**
2   Feb 30  20
2   Mar 20  10
2   Oct 40  30
**3 Jan 10  0**
3   Feb 20  10
3   Mar 30  20
3   Oct 40  30
3   Dec 50  40

how can I calculate delta column?

I couldn't find it anywhere. Please let me know. how to calculate


Solution

  • Subtract column C by Series.sub with repeated first values per groups by GroupBy.transform and GroupBy.first:

    df['Delta'] = df['C'].sub(df.groupby('A')['C'].transform('first'))
    print (df)
        A    B   C  Delta
    0   1  Jan  10      0
    1   1  Feb  20     10
    2   1  Mar  40     30
    3   2  Jan  10      0
    4   2  Feb  30     20
    5   2  Mar  20     10
    6   2  Oct  40     30
    7   3  Jan  10      0
    8   3  Feb  20     10
    9   3  Mar  30     20
    10  3  Oct  40     30
    11  3  Dec  50     40
    

    Detail:

    print (df.groupby('A')['C'].transform('first'))
    0     10
    1     10
    2     10
    3     10
    4     10
    5     10
    6     10
    7     10
    8     10
    9     10
    10    10
    11    10
    Name: C, dtype: int64