python - 应用 scipy 过滤器函数(使用 NAN)后保留 pandas 行号
问题描述
给定熊猫数据框:
PT011
0 -0.160
1 -0.162
2 NaN
3 -0.164
4 NaN
5 NaN
6 -0.166
7 -0.167
删除 NaN 后...:
signal_PT011:
0 -0.160
1 -0.162
3 -0.164
6 -0.166
7 -0.167
我应用 scipy.butter 函数。我想保留熊猫行号,因为过滤后的数据应该回到新列中的原始熊猫数组,与“旧”行号对齐。当我应用 scipy.butter 函数时,我得到的列表没有行数:
Filtered signal PT011:
[-3.86174478e-05 -1.91854502e-04 -4.94647878e-04 -9.42136953e-04
-1.52929127e-03]
作为我期望以下输出熊猫数据框的效果:
Expected output:
PT011 signal_PT011_filtered
0 -0.160 -3.86174478e-05
1 -0.162 -1.91854502e-04
2 NaN NaN
3 -0.164 -4.94647878e-04
4 NaN NaN
5 NaN NaN
6 -0.166 -9.42136953e-04
7 -0.167 -1.52929127e-03
这是我的完整代码:
import pandas as pd
import numpy as np
from scipy import signal
from scipy.signal import butter, lfilter
probes = {'PT011': [-0.16,-0.162,np.NaN,-0.164,np.NaN,np.NaN,-0.166,-0.167]}
df = pd.DataFrame(probes,columns= ['PT011'])
print(df) # Pandas dataframe
df=df.dropna() #Drop NaNs
signal_PT011=df.loc[:,'PT011']
print("Type of signal is:\n",type(signal_PT011)) # Pandas series
print("signal_PT011:\n",signal_PT011)
def butter_lowpass(cutoff, fs, order=2):
nyq = 0.5 * fs
normal_cutoff = cutoff / nyq
b, a = butter(order, normal_cutoff, btype='low', analog=False)
return b, a
def butter_lowpass_filter(data, cutoff, fs, order=2):
b, a = butter_lowpass(cutoff, fs, order=order)
y = lfilter(b, a, data)
return y
order = 2
fs = 100.0 # sample rate, Hz
cutoff = 0.5 # desired cutoff frequency of the filter, Hz
signal_PT011_filtered = butter_lowpass_filter(signal_PT011, cutoff, fs, order) # Execute function to filter data:
print("Filtered signal PT011:\n",signal_PT011_filtered)
我应该怎么办?
解决方案
使用过滤器函数索引的输入作为提供的索引创建一个新系列:
input_to_filter = ... # however it is you acquire it
output_of_filter = ...
new_output = pd.Series(output_of_filter, index=input_to_filter.index)
推荐阅读
- mysql - 如何修复仅返回 0
- deep-learning - 使用在不同数据集上训练的 cnn 模型
- sql - 需要用于 URL 检查的正则表达式?
- jquery - 500 internal server error......Spatie media throwing file not found 异常
- java - Java如何防止使用Random类创建重复数字?
- database - 主键约束的最佳实践?
- kubernetes - GCP 上的 Kubernetes 存储:此上下文中不允许映射值错误
- c# - 为什么会出现错误,说变量未定义。(统一)
- date - 如何在 CMD 中复制伪变量?我正在尝试自动将部分日期复制到剪贴板
- autodesk-forge - 鼠标单击与查看器。选择功能选择在 Autodesk Forge 多模型查看器中的行为不同