给出熊猫数据框:
PT011
0 -0.160
1 -0.162
2 NaN
3 -0.164
4 NaN
5 NaN
6 -0.166
7 -0.167
删除NaNs之后...:
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)
我该怎么办?
答案 0 :(得分:1)
使用过滤器功能的索引输入作为提供的索引来制作新的序列:
input_to_filter = ... # however it is you acquire it
output_of_filter = ...
new_output = pd.Series(output_of_filter, index=input_to_filter.index)