koPytok
koPytok

Reputation: 3713

Calculate differences between elements in group

Consider dataframe with exactly two rows per category in a:

d = pd.DataFrame({"a": ["a", "b", "c", "a", "b", "c"], "b": [1, 3, 1, 4, 2, 6]})
>   a   b
0   a   1
1   b   3
2   c   1
3   a   4
4   b   2
5   c   6

I want to calculate absolute differences between b per a:

    a   b
0   a   3
1   b   1
2   c   5

Upvotes: 1

Views: 127

Answers (2)

koPytok
koPytok

Reputation: 3713

Since each category in a has exactly 2 rows, the dataframe can be split into two like below:

first  = d.drop_duplicates("a", keep="first")
second = d.drop_duplicates("a", keep="last")

Then merge those and calculate the difference:

merged = first.merge(second, on="a")
(merged.b_x - merged.b_y).abs()

Upvotes: 1

Jan Zeiseweis
Jan Zeiseweis

Reputation: 3738

You can group them and then calculate the diff and abs (chain):

d.groupby('a').diff().abs().dropna()

The index might not be exactly as requested but this you can probably figure out.

Upvotes: 2

Related Questions