Python 函数中的“for”循环

Python 函数中的“for”循环,python,pandas,function,for-loop,Python,Pandas,Function,For Loop,我有一个数据框,有四列:Lat、Long、age、age\u。我想写一个函数,它包含一个for循环,应用于数据帧的age\s列 以下是我迄今为止编写的代码,但它不起作用: def bathy(row): x = 0 for a in index: x += (1. / ((1. + 2. * a)**2.))* (math.exp(-(k*((1.+2.*a)**2.)*(pi**2.)*row['age_s'])/(zp**2.))) return (z

我有一个数据框,有四列:Lat、Long、age、age\u。我想写一个函数,它包含一个for循环,应用于数据帧的age\s列

以下是我迄今为止编写的代码,但它不起作用:

def bathy(row):
    x = 0
    for a in index:
        x += (1. / ((1. + 2. * a)**2.))* (math.exp(-(k*((1.+2.*a)**2.)*(pi**2.)*row['age_s'])/(zp**2.)))
    return (zr + (1. - (8. / pi**2. * x)) * (((rhom * ap * deltaT * zp) / (2. * (rhom -rhow)))))/ 1e3

tmp['bathy'] = tmp.apply(bathy, axis=1)
有人能指出我哪里出错了吗

谢谢

给你

tmp['bathy'] = tmp.apply(lambda row: bathy(row), axis=1)

请在问题中发布您的错误回溯。这有助于其他人更好地理解问题!谢谢