How to calculate the difference of a value for multiple dates?
You'll get a better answer if you can provide the expected output. But a simple way of checking the difference between one year from the next on the same day is using groupby
and diff
.
import pandas as pd
df = pd.read_clipboard()
df['gasDayStartedOn'] = pd.to_datetime(df.gasDayStartedOn)
df = df.sort_values(by='gasDayStartedOn', ascending=True)
group = df.groupby([df.gasDayStartedOn.dt.day, df.gasDayStartedOn.dt.month, 'facility'])
df['diff'] = group['gasInStorage'].diff()
df
Out[1]:
facility gasDayStartedOn gasInStorage full injection diff
2 UGS Haidach 2021-01-09 5.5678 43 0.0 NaN
1 UGS Haidach 2022-01-08 4.3263 38 0.0 NaN
0 UGS Haidach 2022-01-09 4.3041 37 0.0 -1.2637