0

I have a DataFrame like this:

Date  Close Symbol
0 2018-03-05  44.21   AAPL
1 2018-03-06  44.17   AAPL
2 2018-03-07  43.76   AAPL
3 2018-03-08  44.24   AAPL
4 2018-03-09  44.99   AAPL
5 2018-03-12  45.43   AAPL
6 2018-03-13  44.99   AAPL
7 2018-03-14  44.61   AAPL
8 2018-03-15  44.66   AAPL
9 2018-03-16  44.51   AAPL
...
2506 2023-02-16  50.99   CSCO
2507 2023-02-17  50.77   CSCO
2508 2023-02-21  49.69   CSCO
2509 2023-02-22  49.31   CSCO
2510 2023-02-23  49.21   CSCO
2511 2023-02-24  48.48   CSCO
2512 2023-02-27  48.73   CSCO
2513 2023-02-28  48.42   CSCO
2514 2023-03-01  48.34   CSCO
2515 2023-03-02  48.53   CSCO

I need to take the daily percent change of each Symbol, replace the Close column with that value, and then bring back the result in this Date Close Symbol format.

I have tried groupby following this post, but I can't quite get it to work.

Ivan
  • 7,448
  • 14
  • 69
  • 134

1 Answers1

1

If possible, I suggest that you use a new column name for the percentage change. Naming it Close makes things rather confusing.

You can try this:

# or df["Close"] = ...
df["Change"] = df.groupby("Symbol")["Close"].pct_change()
Code Different
  • 90,614
  • 16
  • 144
  • 163