How to add a calculated column in a Pandas dataframe?
Try:
# Toy dataset:
air = pd.DataFrame({&quot;Date&quot;:[&quot;2020-02-23&quot;,&quot;2020-04-04&quot;,&quot;2020-04-24&quot;,&quot;2020-01-14&quot;,&quot;2020-02-07&quot;],
&quot;Country&quot;:[&quot;CR&quot;,&quot;CR&quot;,&quot;CR&quot;,&quot;CR&quot;,&quot;CR&quot;],
&quot;count&quot;:[20,23,23,24,23],
&quot;max&quot;:[13.0,17.0,30.0,13.0,57.0 ],
&quot;min&quot;:[20,23,23,24,23],
&quot;median&quot;:[53.0,57.0,80.0,34.0,95.0]})

Input:

# Index column needs to be a datetime so
air[&quot;Date&quot;] = pd.to_datetime(air[&quot;Date&quot;])

# Group by year
air.set_index(&quot;Date&quot;).groupby(pd.Grouper(freq = &quot;y&quot;))[[&quot;count&quot;,&quot;min&quot;,&quot;max&quot;]].diff()

Output: