Python 将函数(不使用lambda)与数据帧的apply方法一起使用

Python 将函数(不使用lambda)与数据帧的apply方法一起使用,python,pandas,Python,Pandas,我正在尝试根据dataframe中的现有列添加列,如下所示。 由于有很多if case,我将if case定义为函数(getDirection方法),并尝试通过apply方法(addDirection方法)调用它。 然而,我有以下错误 TypeError: getDirection() takes 1 positional argument but 2 were given 有人能告诉我如何通过应用数据帧调用函数吗? 代码如下所示 def addDirection(self): g

我正在尝试根据dataframe中的现有列添加列,如下所示。
由于有很多if case,我将if case定义为函数(getDirection方法),并尝试通过apply方法(addDirection方法)调用它。
然而,我有以下错误

TypeError: getDirection() takes 1 positional argument but 2 were given  
有人能告诉我如何通过应用数据帧调用函数吗?
代码如下所示

def addDirection(self):
    group=self.group
    group['direction']=group['Azimuth [deg]'].apply(self.getDirection)

   def getDirection(angle):
        if angle < 11.25 or angle >= 348.75:
            return "N"
        elif angle < 33.75 and angle >= 11.25:
            return "NNE"
        elif angle < 56.25 and angle >= 33.75:
            return "NE"
        elif angle < 78.75 and angle >= 56.25:
            return "ENE"
        elif angle < 101.25 and angle >= 78.75:
            return "E"
        elif angle < 123.75 and angle >= 101.25:
            return "ESE"
        elif angle < 146.25 and angle >= 123.75:
            return "SE"
        elif angle < 168.75 and angle >= 146.25:
            return "SSE"
        elif angle < 191.25 and angle >= 168.75:
            return "S"
        elif angle < 213.75 and angle >= 191.25:
            return "SSW"
        elif angle < 236.25 and angle >= 213.75:
            return "SW"
        elif angle < 258.75 and angle >= 236.25:
            return "WSW"
        elif angle < 281.25 and angle >= 258.75:
            return "W"
        elif angle < 303.75 and angle >= 281.25:
            return "WNW"
        elif angle < 326.25 and angle >= 303.75:
            return "NW"
        elif angle < 348.75 and angle >= 326.25:
            return "NNW"
def addDirection(自):
组=自我组
组['direction']=组['Axitation[deg]']。应用(self.getDirection)
方向(角度):
如果角度<11.25或角度>=348.75:
返回“N”
elif角度<33.75且角度>=11.25:
返回“NNE”
elif角度<56.25且角度>=33.75:
返回“NE”
elif角度<78.75且角度>=56.25:
返回“ENE”
elif角度<101.25且角度>=78.75:
返回“E”
elif角度<123.75且角度>=101.25:
返回“ESE”
elif角度<146.25且角度>=123.75:
返回“SE”
elif角度<168.75且角度>=146.25:
返回“SSE”
elif角度<191.25且角度>=168.75:
返回“S”
elif角度<213.75且角度>=191.25:
返回“SSW”
elif角度<236.25且角度>=213.75:
返回“SW”
elif角度<258.75且角度>=236.25:
返回“WSW”
elif角度<281.25且角度>=258.75:
返回“W”
elif角度<303.75且角度>=281.25:
返回“WNW”
elif角度<326.25且角度>=303.75:
返回“NW”
elif角度<348.75且角度>=326.25:
返回“NNW”

调用
self.getDirection(a)
时,您是隐式调用
getDirection(self,a)
。这就是为什么错误消息指出给出了两个参数而不是一个(
self
和序列的元素)

调用函数的方式(在self.前面加上前缀)意味着它不是一个静态方法,而是一个实例方法。实例方法是在类内部定义的;它们的结果通常取决于调用它的对象(类的实例)的内部状态

如果错误地将函数作为实例方法调用,应该编写
apply(getDirection)
而不是
apply(self.getDirection)
。这很可能是您想要的,因为函数的结果只取决于角度,而不取决于任何对象的内部状态

如果希望
getDirection
成为实例方法,将函数定义为
def getDirection(self,a)
。你不必再做任何改变


另请参见。

调用
self.getDirection(a)
时,您是隐式调用
getDirection(self,a)
。这就是为什么错误消息指出给出了两个参数而不是一个(
self
和序列的元素)

调用函数的方式(在self.前面加上前缀)意味着它不是一个静态方法,而是一个实例方法。实例方法是在类内部定义的;它们的结果通常取决于调用它的对象(类的实例)的内部状态

如果错误地将函数作为实例方法调用,应该编写
apply(getDirection)
而不是
apply(self.getDirection)
。这很可能是您想要的,因为函数的结果只取决于角度,而不取决于任何对象的内部状态

如果希望
getDirection
成为实例方法,将函数定义为
def getDirection(self,a)
。你不必再做任何改变


另请参见。

正如@Cornflex所说,这是因为您正在使用self传递额外的参数,所以出现了该错误。执行
def getDirection(self,angle)
或只执行
apply.(getDirection)

因为它是熊猫,而不是那么多的if-else,我们可以用pd.cut,即

df = pd.DataFrame({'Azimuth [deg]':[300,340,150]})
bins = [11.25, 33.75, 56.25, 78.75, 101.25, 123.75, 146.25, 168.75, 191.25, 213.75, 236.25, 258.75, 281.75, 303.75, 326.25, 348.75]

labels = ['NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW']

pd.cut(df['Azimuth [deg]'],bins=bins,labels=labels).fillna('N') # fillna is for your first condition.

0    WNW
1    NNW
2    SSE
Name: Azimuth [deg], dtype: category
Categories (15, object): [NNE < NE < ENE < E ... W < WNW < NW < NNW]
In [557]:

df['Azimuth [deg]'].apply(getDirection)

0    WNW
1    NNW
2    SSE
Name: Azimuth [deg], dtype: object 
df=pd.DataFrame({'aziration[deg]':[300340150]})
垃圾箱=[11.25,33.75,56.25,78.75,101.25,123.75,146.25,168.75,191.25,213.75,236.25,258.75,281.75,303.75,326.25,348.75]
标签=['NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW']
pd.切割(df['Axitation[deg]',料仓=料仓,标签=标签)。fillna('N')#fillna适用于您的第一个条件。
0西北偏西
西北1
2苏格兰和南方能源公司
名称:方位角[deg],数据类型:类别
类别(15,对象):[NNE
正如@Cornflex所说,这是因为您正在使用self传递额外的参数,所以出现了该错误。执行
def getDirection(self,angle)
或只执行
apply.(getDirection)

因为它是熊猫,而不是那么多的if-else,我们可以用pd.cut,即

df = pd.DataFrame({'Azimuth [deg]':[300,340,150]})
bins = [11.25, 33.75, 56.25, 78.75, 101.25, 123.75, 146.25, 168.75, 191.25, 213.75, 236.25, 258.75, 281.75, 303.75, 326.25, 348.75]

labels = ['NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW']

pd.cut(df['Azimuth [deg]'],bins=bins,labels=labels).fillna('N') # fillna is for your first condition.

0    WNW
1    NNW
2    SSE
Name: Azimuth [deg], dtype: category
Categories (15, object): [NNE < NE < ENE < E ... W < WNW < NW < NNW]
In [557]:

df['Azimuth [deg]'].apply(getDirection)

0    WNW
1    NNW
2    SSE
Name: Azimuth [deg], dtype: object 
df=pd.DataFrame({'aziration[deg]':[300340150]})
垃圾箱=[11.25,33.75,56.25,78.75,101.25,123.75,146.25,168.75,191.25,213.75,236.25,258.75,281.75,303.75,326.25,348.75]
标签=['NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW']
pd.切割(df['Axitation[deg]',料仓=料仓,标签=标签)。fillna('N')#fillna适用于您的第一个条件。
0西北偏西
西北1
2苏格兰和南方能源公司
名称:方位角[deg],数据类型:类别
类别(15,对象):[NNE
您是如何
应用
getDirection()
方法的?
getDirection
似乎是某个类中的一个方法,而不是一个