python-3.x - 使用对数平均值重新采样(或循环)
问题描述
有没有办法使用对数平均值重新采样?我已阅读重采样文档,但找不到对数均值重采样的任何选项。
我有一个带有日期时间索引的大型数据框,每分钟都有观察结果。我需要为一系列变量(列)计算每 5 分钟的对数平均值。
我在下面提供了一些代码,显示了一些示例数据和我想要执行的计算。可能是,如果没有“开箱即用”的对数均值重采样函数,我将需要编写一个循环来执行此操作...?
import numpy as np
import pandas as pd
df1 = pd.DataFrame({'db' : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ]}, index=pd.date_range('2019-05-02T00:00:00', '2019-05-02T00:14:00', freq='1T'))
df1 = df1.resample('5T').mean() # <------ is there a way to do log mean for this?
# The calculation i am need to do is:
df2 = np.log10(10**((df1[observation minute 1]/10)) + 10**((df1[observation minute 2]/10)) + 10**((df1[observation minute 3]/10)) + 10**((df1[observation minute 4]/10)) + 10**((df1[observation minute 5]/10)))
# Where 'observation minute 1,2,3,4,5' are the 5 minutes i want to resample for.
# The resulting df i need is:
df_result = pd.DataFrame({'log_mean' : [np.log10(10**((1/10)) + 10**((2/10)) + 10**((3/10)) + 10**((4/10)) + 10**((5/10))), np.log10(10**((6/10)) + 10**((7/10)) + 10**((8/10)) + 10**((9/10)) + 10**((10/10))), np.log10(10**((11/10)) + 10**((12/10)) + 10**((13/10)) + 10**((14/10)) + 10**((15/10)))]}, index=pd.date_range('2019-05-02T00:00:00', '2019-05-02T00:14:00', freq='5T'))
任何指导将不胜感激。
解决方案
事实证明,您可以使用您选择的任何功能重新采样apply
:
df1 = df1.resample('5T').apply(lambda spl: 10*np.log10(np.mean(np.power(10, spl/10))))
或者您可以单独定义它
def log_avg(spl_arraylike):
return 10*np.log10(np.mean(np.power(10, spl_arraylike/10))))
df1 = df1.resample('5T').apply(log_avg)
这将返回具有以下值的数据帧
2019-05-02 00:00:00 3.227668
2019-05-02 00:05:00 8.227668
2019-05-02 00:10:00 13.227668
推荐阅读
- sql - 我们可以在 SQL Server 中为每个计数使用多个计数(列)和 where cluase
- python - 我如何删除 python 2.7,因为我已经在 ubuntu 上安装了 3.6.5?
- go - 如何避免在连续错误处理中重复自己
- python - 如何调试通过 Jenkins 运行 shell 脚本的问题?
- csom - 如何通过 csom 代码使用保存的模板创建任务列表
- block - 如何以编程方式显示自定义块
- jenkins - 詹金斯管道 - 无法在空对象上调用方法阶段()
- swift - 集合视图单元格动态高度问题
- java - 获取响应的总页数
- go - Golang Program leaking memory