简体   繁体   English

Savgol 过滤 dataframe 列

[英]Savgol filter over dataframe columns

I'm trying to apply a savgol filter from SciPy to smooth my data.我正在尝试应用 SciPy 中的 savgol 过滤器来平滑我的数据。 I've successfully applied the filter by selecting each column separately, defining a new y value and plotting it.通过分别选择每一列、定义一个新的 y 值并绘制它,我已经成功地应用了过滤器。 However I wanted to apply the function in a more efficient way across a dataframe.但是,我想以更有效的方式在 dataframe 中应用 function。

y0 = alldata_raw.iloc[:,0]
w0 = savgol_filter(y0, 41, 1)

My first thought was to create an empty array, write a for loop apply the function to each column, append it to the array and finally concatenate the array.我的第一个想法是创建一个空数组,编写一个 for 循环,将 function 应用于每一列,将 append 应用于数组,最后连接数组。 However I got an error 'TypeError: cannot concatenate object of type "";但是我收到一个错误'TypeError:无法连接“”类型的 object; only pd.Series, pd.DataFrame, and pd.Panel (deprecated) objs are valid'只有 pd.Series、pd.DataFrame 和 pd.Panel(已弃用)对象有效'

smoothed_array = []
for key,values in alldata_raw.iteritems():
    y = savgol_filter(values, 41, 1)
    smoothed_array.append(y)

alldata_smoothed = pd.concat(smoothed_array, axis=1)

Instead I tried using the pd.apply() function however I'm having issues with that.相反,我尝试使用 pd.apply() function 但是我遇到了问题。 I have an error message: 'TypeError: expected x and y to have same length'我有一条错误消息:“TypeError:预期 x 和 y 的长度相同”

alldata_smoothed = alldata_raw.apply(savgol_filter(alldata_raw, 41, 1), axis=1)
print(alldata_smoothed)

I'm quite new to python so any advice on how to make each method work and which is preferable would be appreciated!我对 python 很陌生,所以任何关于如何使每种方法工作的建议以及哪种方法更可取将不胜感激!

In order to use the filter first create a function that takes a single argument - the column data.为了使用过滤器,首先创建一个采用单个参数的 function - 列数据。 Then you can apply it to dataframe columns like this:然后您可以将其应用于 dataframe 列,如下所示:

from scipy.signal import savgol_filter
def my_filter(x):
    return savgol_filter(x, 41, 1)
alldata_smoothed = alldata_raw.apply(my_filter)

You could also go with a lambda function:您也可以使用 go 和lambda function:

alldata_smoothed = alldata_raw.apply(lambda x: savgol_filter(x,41,1))

axis=1 in apply is specified to apply the function to dataframe rows.指定apply中的axis=1以将 function 应用于 dataframe 行。 What you need is the default option axis=0 which means apply it to the columns.您需要的是默认选项axis=0 ,这意味着将其应用于列。

That was pretty general but the docs for savgol_filter tell me that it accepts an axis argument too.这很笼统,但savgol_filter文档告诉我它也接受axis参数。 So in this specific case you could apply the filter to the whole dataframe at once.因此,在这种特定情况下,您可以一次将过滤器应用于整个 dataframe。 This will probably be more performant but I haven't checked =).这可能会更高效,但我还没有检查 =)。

alldata_smoothed = pd.DataFrame(savgol_filter(alldata_raw, 41, 1, axis=0),
                                columns=alldata_raw.columns,
                                index=alldata_raw.index)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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