'Calculate percentage change for an item in a dataframe that contains multiple items
I have a dataset with symbol, date and oi. I populate the oi every day for the symbol. I would like to calculate the percentage change in oi from the previous day for all the symbols.
DATE OI
SYMBOL
HINDALCO 21-FEB-2022 67815300
FSL 21-FEB-2022 29320200
FEDERALBNK 22-FEB-2022 151660000
HINDALCO 22-FEB-2022 114510
FSL 22-FEB-2022 76852100
Output
SYMBOL DATE OI. #changeOI
HINDALCO 22-FEB-2022 114510 value
FSL 22-FEB-2022 76852100 value
Solution 1:[1]
Figured this out using pivot table
df1 = (df.pivot_table(index='SYMBOL',
columns='DATE',
values='OI',
aggfunc='sum')
.pct_change(axis=1))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Vaibhav Jha |
