Python은 클래스에 메서드를 정의하고 메서드는 함수를 호출해야 합니다.

class group():
    # 省略
    def get_group_scale(self):
       
        '''
        省略
        ''' 
        df['分类'] = df['number'].apply( self.group_number ) 
        return df
        

시나리오는 0-9, 10-19 등의 숫자에 따라 그룹화하고 그룹화 결과를 반환해야 한다는 것입니다.

    def group_number( number ):
        '''
        将团队规模进行分组,按照0-9、9-19、....、100-109分组统计。
        group_number :团队规模
        :return:
        '''
        ###根据团队规模返回分组,分组包括0-9、9-19、.....、100-109
        quotient = number // 10  # 获取商
        c = {
    
    }
        for i in range(0, 11):
            value = str(i * 10) + '至' + str((i + 1) * 10 - 1)
            c[i] = value
        return c[quotient]
    
    '''   
    省略
    ''' 
    df['分类'] = df['number'].apply( group_number ) 

이제 클래스를 작성한 후 클래스에서 호출하려면 @staticmethod를 추가해야 합니다. 그렇지 않으면 오류를 보고합니다.

class group():
    '''
    略
    '''
    @staticmethod
    def group_number( number ):
        '''
        按照0-9、9-19、....、100-109分组统计。
        '''
        ###根据团队规模返回分组,分组包括0-9、9-19、.....、100-109
        quotient = number // 10  # 获取商
        c = {
    
    }
        for i in range(0, 11):
            value = str(i * 10) + '至' + str((i + 1) * 10 - 1)
            c[i] = value
        return c[quotient]

    def get_group_scale(self):
        '''
        略
        '''
        df['分类'] = df['number'].apply( self.group_number )
         
        return df

おすすめ

転載: blog.csdn.net/u012076669/article/details/126686086