python - Pandas 使用 numpy 百分位数重采样?

标签 python numpy pandas

在使用pandas函数resample时,你有没有用过numpy的percentile函数??

考虑到“数据”是一个只有一列包含 10 分钟数据的数据框,我想做这样的事情:

dataDaily=data.resample('D',how=np.percentile(data['Col1'],q=90)

我收到以下错误:

'numpy.float64' object is not callable

你试过吗?

最佳答案

您必须将函数传递给how 参数,而不是值。我认为在你的情况下你可以使用匿名函数(lambda 函数):

dataDaily = data.resample('D', how=lambda x: np.percentile(x['Col1'], q=90))

例子:

>>> df = pd.DataFrame({'Col1': np.random.randn(10)})
>>> df.index = map(pd.Timestamp, ['20130101', '20130102']) * 5)
>>> df
                Col1
2013-01-01 -0.636815
2013-01-02 -0.028921
2013-01-01  0.643083
2013-01-02  0.065096
2013-01-01  0.446963
2013-01-02  0.462307
2013-01-01  2.768514
2013-01-02 -1.406168
2013-01-01  0.732656
2013-01-02 -0.699028
>>> df.resample('D', how=lambda x: np.percentile(x['Col1'], q=90))
                Col1
2013-01-01  1.954171
2013-01-02  0.303423

关于python - Pandas 使用 numpy 百分位数重采样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19703179/

相关文章:

python - django.db.utils.IntegrityError : NOT NULL constraint failed: users_profile. user_id

Python - 如何使用 python 在 word 文档中嵌入 OLE

Python 二进制搜索总是返回目标未找到的值

python - matplotlib 无法绘制单 channel 或灰度图像像素

python - 在 Python 请求模块中处理自动重定向

python - 使用 numpy 查找最近的位置

python - 均匀生成不同整数的随机对

python - 解决 Python 代码中的 TypeError

python - 从其他数据框 Pandas 填充数据框中一列的 NAN 值

python - 在 python 中使用 pandas 合并行数据