Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
居中移动窗口百分位-Python_Python_Pandas_Numpy - Fatal编程技术网

居中移动窗口百分位-Python

居中移动窗口百分位-Python,python,pandas,numpy,Python,Pandas,Numpy,我有以下清单: a = [80, 79, 30, 15, 12, 14, 20, 15, 10, 45, 52, 59, 51, 60, 72, 77, 60, 15, 20, 10, 12] 我想得到一个与“a”长度相同的列表“b”,其中每个索引显示一个中心移动窗口,每个索引的+-3个位置占10% 因此,我对以下列表感兴趣: b[0] = np.percentile(a[0:2], 10) b[1] = np.percentile(a[0:3], 10) b[2] = np.percenti

我有以下清单:

a = [80, 79, 30, 15, 12, 14, 20, 15, 10, 45, 52, 59, 51, 60, 72, 77, 60, 15, 20, 10, 12]
我想得到一个与“a”长度相同的列表“b”,其中每个索引显示一个中心移动窗口,每个索引的+-3个位置占10%

因此,我对以下列表感兴趣:

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)

.....

谢谢

将列表转换为数据帧

df=pd.DataFrame(a).reset_index()
然后我们使用
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

谢谢x-3和x+2是否表示窗口大小?@hpnk85是。。这与您的示例相匹配:-)您可以更改它