I have the following dataframe structure
Company Name stock price Date
HP 1.2 10/05/2020
HP 1.4 11/05/2020
APPL 1.1 05/03/2020
APPL 1.2 06/03/2020
FB 5 15/08/2020
FB 5.2 16/08/2020
FB 5.3 17/08/2020
...and so on for multiple companies and their stock prices for different dates.
I wish to calculate daily returns for each stock and I am trying to figure out the code to iterate this dataframe for each company. I.e. when we are done with APPL we start again over for FB by setting the first row to N/A since we don't have returns to compare with, and so on as shown below.
Company Name stock price Date Daily Returns
HP 1.2 10/05/2020 N/A
HP 1.4 11/05/2020 0.2
APPL 1.1 05/03/2020 N/A
APPL 1.2 06/03/2020 0.1
FB 5 15/08/2020 N/A
FB 5.2 16/08/2020 0.2
FB 5.3 17/08/2020 0.1
Is there a more efficient solution to tackle this than extracting a list of unique company names and then cycling through each of them to perform this calculation?