简体   繁体   中英

How to apply smoothing function in pandas column using group by?

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.

在此处输入图像描述

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 technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM