首页 > 解决方案 > 应用 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)

我应该怎么办?

标签: pythonpandasscipy

解决方案


使用过滤器函数索引的输入作为提供的索引创建一个新系列:

input_to_filter = ...  # however it is you acquire it
output_of_filter = ...
new_output = pd.Series(output_of_filter, index=input_to_filter.index)

推荐阅读