0

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.

enter image description here

Sangeetha R
  • 139
  • 7
  • What does "some issue in table indexing" mean? Please provide a [complete and reproducible example](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). – Mr. T Dec 26 '22 at 10:59

1 Answers1

0

df.assign takes dict or tuple etc but not array, I got err there. Instead, you can distribute the data in the smooth_data array and create a new dataframe.

Here:

df=pd.DataFrame({'id':df['id'],'smooth_data':[x for x in smooth_data.tolist()]})

Output :

>>>      id  smooth_data
>>> 0   id1    -2.469418
>>> 1   id1    -2.467627
>>> 2   id1    -2.465924
>>> 3   id1    -2.464312
>>> 4   id1    -2.462790
>>> 5   id1    -2.461360
>>> 6   id1    -2.460020
>>> 7   id1    -2.458772
>>> 8   id1    -2.457614
>>> 9   id1    -2.456545
>>> 10  id2    -2.455564
>>> 11  id2    -2.454670
>>> 12  id2    -2.453859
>>> 13  id2    -2.453131
>>> 14  id2    -2.452481
>>> 15  id2    -2.451907
>>> 16  id2    -2.451405
>>> 17  id2    -2.450973
>>> 18  id2    -2.450606
>>> 19  id2    -2.450299
The Lord
  • 72
  • 1
  • 7