Python – Use partitions to calculate deltas between entries in Pandas

Use partitions to calculate deltas between entries in Pandas… here is a solution to the problem.

Use partitions to calculate deltas between entries in Pandas

I’m using a Dataframe in Pandas and I want to use partitions to calculate the delta between each adjacent row.

For example, this is my initial collection after sorting by A and B:

    A   B    
1   12  40
2   12  50
3   12  65
4   23  30
5   23  45
6   23  60

I want to calculate the delta between adjacent B values, divided by A. If we define C as the result, the final table should look like this:

    A   B   C   
1   12  40  NaN
2   12  50  10
3   12  65  15
4   23  30  NaN
5   23  45  15
6   23  75  30

The reason for NaN is that we can’t calculate the increment of the smallest number in each partition.

Solution

You can group and take the difference by column A:

df['C'] = df.groupby('A')['B'].diff()

df
Out: 
    A   B     C
1  12  40   NaN
2  12  50  10.0
3  12  65  15.0
4  23  30   NaN
5  23  45  15.0
6  23  60  15.0

Related Problems and Solutions