Q:

How to add column of rolling slope in Pandas

Working with a dataset where I want to get the slope between every point. I can easily accomplish this in Excel for small set. For example where day 10  is my first record:
days count  slope
10  537 
9.5 502 70
9   487 30
8.5 496 -18
8   432 128
7.5 301 262
7   275 52
6.5 265 20
6   236 58
5.5 210 52
5   150 120

Slope 70 at day 9.5 obtained by the following formula in Excel:
=(I2-I3)/(H2-H3)

Since I am working with a larger dataset I would like to accomplish this in Pandas.

A:

You can do:
df['slope'] = df['count'].diff()/df['days'].diff()

Output:
    days  count  slope
0   10.0    537    NaN
1    9.5    502   70.0
2    9.0    487   30.0
3    8.5    496  -18.0
4    8.0    432  128.0
5    7.5    301  262.0
6    7.0    275   52.0
7    6.5    265   20.0
8    6.0    236   58.0
9    5.5    210   52.0
10   5.0    150  120.0

Note: For groupby:
groups = df.groupby('category').diff()
df['slope'] = groups['count']/groups['days']

