I nee to apply butterworth filter on the pandas column grouping by ID
Code:
import pandas as pd
import scipy.signal as signal
data = [['id1', 1], ['id1', 1], ['id1', 5], ['id1', 5],['id1', 5],['id1', 1], ['id1', 1], ['id1', 5], ['id1', 5], ['id1', 5],['id2', 1], ['id2', 2], ['id2', 5], ['id2', 3],['id2', 1], ['id2', 1], ['id2', 2],['id2', 2], ['id2', 5], ['id2', 3]]
df = pd.DataFrame(data, columns=['id', 'raw_data'])
def smoothing(df):
N=2
w=.005
B,A=signal.butter(N, w, output='ba')
smooth_data= signal.filtfilt(B,A,df['raw_data'])
df=df.assign(smooth_data=smooth_data)
df=df.groupby('id').apply(smoothing)
The result obtained is given below. I want this in single dataframe with same indexing as df.