计算动态时间序列的中值

2024-06-17 09:38:33 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我有一个熊猫系列[a1,a2,a3,a4,…],长度=T,每个值对应一天。对于每一天,我要计算历史中值。例如,第一天计算[a1]的中位数;第二天计算[a1,a2]的中位数;第n天计算[a1,a2,…,an]的中位数。最后我想得到一个长度为T的序列。我们有没有一个有效的方法在熊猫身上做到这一点?谢谢!你知道吗


Tags: 方法ana2a1序列历史a3a4
1条回答
网友
1楼 · 发布于 2024-06-17 09:38:33

对于序列,ser

ser = pd.Series(np.random.randint(0, 100, 10))

如果您的pandas版本为0.18.0或更高版本,请使用:

ser.expanding().median()
Out: 
0     0.0
1    25.0
2    50.0
3    36.5
4    33.0
5    36.0
6    33.0
7    36.0
8    33.0
9    36.0
dtype: float64

以下是早期版本,已弃用:

pd.expanding_median(ser)
C:\Anaconda3\envs\p3\lib\site-packages\spyder\utils\ipython\start_kernel.py:1: FutureWarning: pd.expanding_median is deprecated for Series and will be removed in a future version, replace with 
        Series.expanding(min_periods=1).median()
  # -*- coding: utf-8 -*-
Out: 
0     0.0
1    25.0
2    50.0
3    36.5
4    33.0
5    36.0
6    33.0
7    36.0
8    33.0
9    36.0
dtype: float64

相关问题 更多 >