gpt4 book ai didi

python - 居中移动窗口百分位数 - Python

转载 作者:太空宇宙 更新时间:2023-11-03 14:20:18 25 4
gpt4 key购买 nike

我有以下列表:

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

关于python - 居中移动窗口百分位数 - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47984176/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com