Stid Zhang
Stid Zhang

Reputation: 47

Conditional shift in pandas

The following pandas DataFrame is an example that I need to deal with:

       Group   Amount
1      1       100
2      1       300
3      1       400
4      1       700
5      2       500
6      2       900

Here's the result that I want after calculation:

       Group   Amount   Difference
1      1       100      100
2      1       300      200
3      1       400      100
4      1       700      300
5      2       500      500
6      2       900      400

I knew that df["Difference"] = df["Amount"] - df["Amount"].shift(-1) can produce the difference between all rows, but what can I do for the problem I have like this that needs a group as condition?

Upvotes: 1

Views: 1284

Answers (1)

EdChum
EdChum

Reputation: 394003

groupby on 'Group' and call transform on the 'Amount' col, additionally call fillna and pass the 'Amount' column:

In [110]:
df['Difference'] = df.groupby('Group')['Amount'].transform(pd.Series.diff).fillna(df['Amount'])
df
​
Out[110]:
   Group  Amount  Difference
1      1     100         100
2      1     300         200
3      1     400         100
4      1     700         300
5      2     500         500
6      2     900         400

Upvotes: 2

Related Questions