1

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?

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

Is there any way to insert columns witthout naming like [["price", "amount"]]? I have a high dimensional data and I am trying to inject the columns except {"date", "product"}.
Sure, use something like cols = ["price", "amount"] and if you want the other cols then others = [c for c in df.columns if c not in ("price", "amount")]
then you can select all the others with df[others].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.