Python 自定义分箱

sx_apply_info_0427=pd.read_excel(r'E:\lll\202004\sx_info.xlsx')
fx_variable=pd.read_excel(r'E:\lll\202004\fx_variable.xlsx')#获取分箱列表
variable=fx_variable['var'].drop_duplicates().values.tolist()#对变量去重
'''
cut_box=[1,3,5,10]
x<1 返回1
x<3 返回3
不包含下限
'''
for var in variable:
    cut_box=list(fx_variable[fx_variable['var']==var]['下限'])
    def func_(x):
        num_groups =len(cut_box)       
        if x< cut_box[1]: 
            return cut_box[0]
        for i in range(1,num_groups):
            if cut_box[i-1] <= x < cut_box[i]:
                return cut_box[i-1]     
        return cut_box[num_groups-1]   
    sx_info[var]=sx_info[var].apply(func_,)

你可能感兴趣的:(python,学习)