CHRD
CHRD

Reputation: 1957

Pandas groupby and diff based on two columns

Suppose I have the following dataframe:

df = pd.DataFrame(
    {
        'A': ['a', 'a', 'b', 'b'],
        'B': [10, 8, 6, 4],
        'C': [9, 7, 5, 3]
    }
)

I want to achieve this:

df.groupby('A').B.apply(lambda x: x.diff())

But I would like the diff() to be between B and C instead of between B and B (hope this makes sense). I can do something like this to achieve the diff() I'm after:

df.C.shift(-1)-df.B

But I'm stuck on how I should incorporate it in my groupby logic.

The end result would look like:

0    NaN
1   -3.0
2    NaN
3   -3.0

Any ideas?

Upvotes: 0

Views: 60

Answers (1)

Shubham Sharma
Shubham Sharma

Reputation: 71687

IIUC, Use:

s = df.groupby('A').apply(lambda x: x.C - x.B.shift()).reset_index(drop=True)
print(s)

This prints:

0    NaN
1   -3.0
2    NaN
3   -3.0

Upvotes: 2

Related Questions