hpnk85
hpnk85

Reputation: 395

Centred Moving Window percentiles - Python

I have the following list:

a = [80, 79, 30, 15, 12, 14, 20, 15, 10, 45, 52, 59, 51, 60, 72, 77, 60, 15, 20, 10, 12]

I would like to get a list 'b' with the same length as 'a' where each index shows a Centred Moving Window 10th percentile of +-3 locations of each index.

So I am interested in a list that shows the following:

b[0] = np.percentile(a[0:2], 10)
b[1] = np.percentile(a[0:3], 10)
b[2] = np.percentile(a[0:4], 10)
b[3] = np.percentile(a[0:5], 10)
b[4] = np.percentile(a[1:6], 10)
b[5] = np.percentile(a[2:7], 10)

.....

Thanks

Upvotes: 1

Views: 76

Answers (1)

BENY
BENY

Reputation: 323266

Convert your list to dataframe

df=pd.DataFrame(a).reset_index()

Then we using apply

df['index'].apply(lambda x : np.percentile(df[0].iloc[np.clip(x-3,0,None):x+2],10))
Out[1429]: 
0     79.1
1     39.8
2     19.5
3     13.2
4     12.8
5     12.8
6     12.8
7     10.8
8     11.6
9     12.0
10    12.0
11    24.0
12    47.4
13    51.4
14    54.2
15    54.6
16    33.0
17    17.0
18    12.0
19    10.8
20    10.6
Name: index, dtype: float64

Upvotes: 1

Related Questions