python - 对一系列系列应用自动更正

标签 python pandas

我有一个包含多个时间序列的数据框:

df.head()
             0         1         2
time                              
0.0   0.365035  0.365035  0.365035
1.0   0.349999  0.349999  0.349999
2.0   0.340998  0.340998  0.340998
3.0   0.333877  0.333877  0.333877
4.0   0.326411  0.326411  0.326411

现在我想计算每个的 stdautocorr

我知道我可以单独完成:

df[0].aggregate(['std', 'autocorr'])
Out[10]: 
std         0.081165
autocorr    0.995285

对于std,它有效:

df.unstack().groupby(level=0).aggregate(['std'])
Out[11]: 
        std
0  0.081165
1  0.081165
2  0.081165

但是当我尝试对 autocorr 做同样的事情时,我得到了

df.unstack().groupby(level=0).aggregate(['autocorr'])
AttributeError: Cannot access callable attribute 'autocorr' of 'SeriesGroupBy' objects, try using the 'apply' method

为什么会发生这种情况?正确的方法/解决方法是什么?

最佳答案

很可能 autocorr 没有作为 SeriesGroupBy 类的方法实现。

试试这个:

In [15]: df.unstack().groupby(level=0).agg(['std', pd.Series.autocorr])
Out[15]:
        std  autocorr
0  0.014972  0.991893
1  0.014972  0.991893
2  0.014972  0.991893

关于python - 对一系列系列应用自动更正,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50444987/

相关文章:

python - Pandas/Numpy - 填充另一列的缺失值

python - Pandas fillna() 未按预期工作

python 使用正则表达式创建新列

pandas - 将索引和列值作为输入对 pandas 数据框应用逐元素函数

python - 需要帮助将 Flask-SQLAlchemy 连接到 MS SQL Server 2008R2

python - 根据行值合并 Pandas 数据框

python - 就地改组多个 HDF5 数据集

python - Pandas Dataframe 到 HTML 删除索引

python - 通过字符串变量 reshape 数据框

python - 使用 JMESPath 基于另一个查询结果过滤列表