I have a data frame like this:
sample = pd.DataFrame({'a': [10, 16, 7, 2, 5]})
How can I find the average distance for the column 'a'? For example, the average distance between 10 and 16 = (10 + 6) / 2 = 8
I have a data frame like this:
sample = pd.DataFrame({'a': [10, 16, 7, 2, 5]})
How can I find the average distance for the column 'a'? For example, the average distance between 10 and 16 = (10 + 6) / 2 = 8
The "average distance" of x and y is defined as (x + (y - x))/2.
That's just y/2. Drop the first row and divide the rest by 2: sample.a[1:]/2
After the math you described, it is equivalent to "next value divided by 2". So we get the next values by shifting the series and then / 2
to halve them:
# data itself
>>> sample
a
0 10
1 16
2 7
3 2
4 5
# shift the values upwards by 1 unit so 2nd becomes 1st etc.
# and the last becomes NaN as there is nothing below it to replace
>>> sample.shift(-1)
a
0 16.0
1 7.0
2 2.0
3 5.0
4 NaN
# per your formula
>>> sample.shift(-1) / 2
a
0 8.0
1 3.5
2 1.0
3 2.5
4 NaN