将数据帧的所有内容传递到 pandas 中的函数 [英] Pass entire contents of a dataframe to a function in Pandas
问题描述
我正在尝试使用Dataframes而不是numpy数组对我的分析代码进行信号处理。但是,我很难弄清楚如何将整个数据帧矩阵作为整个单元传递给一个函数。
I'm trying to rework much of my analysis code for signal processing using Dataframes instead of numpy arrays. However, I'm having a hard time figuring out how to pass the entire matrix of a dataframe to a function as an entire unit.
例如,如果我正在计算通用平均参考值的信号,我有一些像:
E.g., If I'm computing the common average reference a signal, I have something like:
avg = signal.mean(axis=1)
CAR = signal - avg
我想做的是将一个熊猫数组传递给这个函数,并使它返回一个具有CAR值的数据帧作为值。我想这样做,而不用返回一个数组,然后将其重新转换回数据框。
What I'd like to do is pass a pandas array to this function and have it return a dataframe with CAR as the values now. I'd like to do this without just returning an array and then re-converting it back into a dataframe.
这听起来像是使用df.apply()它是行方式或列方式,并不放在整个矩阵中。我可以改变CAR的代码来使这个工作,但它似乎会减慢相当一点,而不是只是使用numpy的代码一次性完成所有这一切。这可能不会对计算平均值产生很大的影响,但是我预计这是未来可能需要更长时间的其他功能的问题。
It sounds like when you use df.apply(), it goes row-wise or column-wise, and doesn't put in the whole matrix. I could alter the code of CAR to make this work, but it seems like it would slow it down quite a bit rather than just using numpy's code to do it all at once. It probably wouldn't make a big difference for computing the mean, but I foresee this being a problem with other functions in the future that might take longer.
任何人都可以指出我在正确的方向?
Can anyone point me in the right direction?
编辑:澄清,我不只是为了减去平均值,这只是一个简单的例子。一个更逼真的例子是沿轴线0对阵列进行线性过滤。我想使用scipy.signal filtfilt函数来过滤我的数组。这很容易,如果我可以传递一个tpts x专长矩阵,但现在似乎唯一的方法是列列使用应用
To clarify, I'm not just doing this for subtracting the mean, it was just a simple example. A more realistic example would be linearly filtering the array along axis 0. I'd like to use the scipy.signal filtfilt function to filter my array. This is quite easy if I can just pass it a tpts x feats matrix, but right now it seems that the only way to do it is column-wise using "apply"
推荐答案
您可以使用 df.values
获取DataFrame的原始numpy数组版本。然而,在许多情况下,您可以传递DataFrame本身,因为它仍然允许使用正常的numpy API(即它具有所有正确的方法)。
You can get the raw numpy array version of a DataFrame with df.values
. However, in many cases you can just pass the DataFrame itself, since it still allows use of the normal numpy API (i.e., it has all the right methods).
这篇关于将数据帧的所有内容传递到 pandas 中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!