如何在Python中对每行数据帧运行函数

如何在Python中对每行数据帧运行函数,python,pandas,for-loop,fft,Python,Pandas,For Loop,Fft,我想在数据集df的每一行上循环一个函数。df为920 x 10080。该函数使用快速傅里叶变换的概念从波(由每行数据形成)中提取前5个频率分量。功能代码: def get_fft_values(y_values, T, N, f_s): f_values = np.linspace(0.0, 1.0/(2.0*T), N//2) fft_values_ = fft(y_values) fft_values = 2.0/N * np.abs(fft_values_[0:N/

我想在数据集df的每一行上循环一个函数。df为
920 x 10080
。该函数使用快速傅里叶变换的概念从波(由每行数据形成)中提取前5个频率分量。功能代码:

def get_fft_values(y_values, T, N, f_s):
    f_values = np.linspace(0.0, 1.0/(2.0*T), N//2)
    fft_values_ = fft(y_values)
    fft_values = 2.0/N * np.abs(fft_values_[0:N//2])
    return f_values, fft_values  #f_values - frequency(length = 5040) ; fft_values - amplitude (length = 5040)   

t_n = 1
N = 10080
T = t_n / N
f_s = 1/T

f_values, fft_values = get_fft_values(df, T, N, f_s)

print(f_values[0:5], fft_values[0:5]
这是使用一行数据作为数据帧的傅里叶变换函数(我切片
df=df.iloc[1,:]
)。我不知道如何每行运行一个函数。我试过了

for row in df.iterrows():

  t_n = 1
  N = 10080
  T = t_n / N
  f_s = 1/T

  f_values, fft_values = get_fft_values(pd.DataFrame(row), T, N, f_s)

  plt.plot(f_values, fft_values, 'x', color='blue', markersize = '3')
  plt.xlabel('Frequency [Hz]', fontsize=16)
  plt.ylabel('Amplitude', fontsize=16)
  plt.title("Frequency domain of the data", fontsize=16)
  plt.show()

  print(f_values[0:5], fft_values[0:5], '\n')
哪个返回了错误

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-158-866f17cf6143> in <module>()
     51   f_s = 1/T
     52 
---> 53   f_values, fft_values = get_fft_values(pd.DataFrame(row), T, N, f_s)
     54 
     55   plt.plot(f_values, fft_values, 'x', color='blue', markersize = '3')

4 frames
/usr/local/lib/python3.6/dist-packages/numpy/core/_asarray.py in asarray(a, dtype, order)
     83 
     84     """
---> 85     return array(a, dtype, copy=False, order=order)
     86 
     87 

ValueError: setting an array element with a sequence.
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-154-241b17a458df> in <module>()
----> 1 results = pd.DataFrame([get_fft_values(*x,T, N, f_s) for [x] in df.values.tolist()])
      2 results = df.apply(lambda x: get_fft_values(*x))
      3 results

<ipython-input-154-241b17a458df> in <listcomp>(.0)
----> 1 results = pd.DataFrame([get_fft_values(*x,T, N, f_s) for [x] in df.values.tolist()])
      2 results = df.apply(lambda x: get_fft_values(*x))
      3 results

ValueError: too many values to unpack (expected 1)
哪个返回了错误

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-158-866f17cf6143> in <module>()
     51   f_s = 1/T
     52 
---> 53   f_values, fft_values = get_fft_values(pd.DataFrame(row), T, N, f_s)
     54 
     55   plt.plot(f_values, fft_values, 'x', color='blue', markersize = '3')

4 frames
/usr/local/lib/python3.6/dist-packages/numpy/core/_asarray.py in asarray(a, dtype, order)
     83 
     84     """
---> 85     return array(a, dtype, copy=False, order=order)
     86 
     87 

ValueError: setting an array element with a sequence.
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-154-241b17a458df> in <module>()
----> 1 results = pd.DataFrame([get_fft_values(*x,T, N, f_s) for [x] in df.values.tolist()])
      2 results = df.apply(lambda x: get_fft_values(*x))
      3 results

<ipython-input-154-241b17a458df> in <listcomp>(.0)
----> 1 results = pd.DataFrame([get_fft_values(*x,T, N, f_s) for [x] in df.values.tolist()])
      2 results = df.apply(lambda x: get_fft_values(*x))
      3 results

ValueError: too many values to unpack (expected 1)
返回的

--------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-178-75f82bbfdaca> in <module>()
     16 
     17 
---> 18 df.apply(get_fft_values, axis=1)

3 frames
/usr/local/lib/python3.6/dist-packages/pandas/core/apply.py in apply_series_generator(self)
    319             try:
    320                 for i, v in enumerate(series_gen):
--> 321                     results[i] = self.f(v)
    322                     keys.append(v.name)
    323             except Exception as e:

TypeError: ("get_fft_values() missing 3 required positional arguments: 'T', 'N', and 'f_s'", 'occurred at index 0')
--------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
16
17
--->18 df.应用(获取fft值,轴=1)
3帧
/usr/local/lib/python3.6/dist-packages/pandas/core/apply.py in-apply_-series_生成器(self)
319试试:
320用于枚举中的i、v(系列):
-->321结果[i]=自f(v)
322个键。追加(v.name)
323例外情况除外,如e:
TypeError:(“get_fft_values()缺少3个必需的位置参数:'T'、'N'和'f_s','发生在索引0'处)

这似乎是一个直截了当的问题,但我不知道如何解决它。在这种情况下,如何编写正确的for循环?非常感谢关于如何在数据帧中的每一行上应用函数的问题,我想给出一个简单的示例,以便您可以相应地更改代码

df = pd.DataFrame(data) ## creating a dataframe

def select_age(row): ##  a function which selects and returns only the names which have age greater than 18. Here the argument is row
                    ## i simply named it as row. So that its easy to understand that each row is being passed.
    boole = 'No'
    age  = row['Age']  ## from each row we are extracting the values of age 
    if age >18:
        boole = 'Yes'
    return boole  ## if age is greater, then we are passing yes for that row

df['Age>18'] = df.apply(lambda x: select_age(x), axis =1) # here we are applying apply function which performs the required action on each row

"""For each row we are passing yes or no and storing it in a new column"""
输出


你做过任何研究吗,读过熊猫文档吗?@AMC我搜索过numpy和熊猫文档、在线博客和其他帖子,这是否回答了你的问题@AMC:没有。请参阅编辑后的问题。感谢查看
.apply()
的文档或链接帖子。这只是从每一行得到正确的参数的问题。我认为lambda在这里是不必要的。谢谢你的回答。我目前的问题是当有多个参数时如何应用函数?