user3104352
user3104352

Reputation: 1130

how to update rows based on previous row of dataframe python

I have a time series data given below:

date    product price   amount
11/01/2019  A   10  20
11/02/2019  A   10  20
11/03/2019  A   25  15
11/04/2019  C   40  50
11/05/2019  C   50  60

I have a high dimensional data, and I have just added the simplified version with two columns {price, amount}. I am trying to transform it relatively based on time index illustrated below:

date    product price   amount
    11/01/2019  A   NaN NaN
    11/02/2019  A   0   0
    11/03/2019  A   15  -5
    11/04/2019  C   NaN NaN
    11/05/2019  C   10  10

I am trying to get relative changes of each product based on time indexes. If previous date does not exist for a specified product, I am adding "NaN".

Can you please tell me is there any function to do this?

Upvotes: 1

Views: 87

Answers (1)

Nathan Furnal
Nathan Furnal

Reputation: 2410

Group by product and use .diff()

df[["price", "amount"]] = df.groupby("product")[["price", "amount"]].diff()

output :

        date product  price  amount
0 2019-11-01       A    NaN     NaN
1 2019-11-02       A    0.0     0.0
2 2019-11-03       A   15.0    -5.0
3 2019-11-04       C    NaN     NaN
4 2019-11-05       C   10.0    10.0

Upvotes: 1

Related Questions