lundi 10 août 2020

python: If statement shows different results in function and direct expression

if all(x1 == x2 for (x1, x2) in zip(a, b)):

This is the line that shows different results in function and direct expression. when I run the thing after if:

but in the function, if judges it as false and directcly pass it, I can't find the reason.

funciton version

def datalist(df):
    df.dropna(axis=1,how = 'all', inplace = True) # 去除空列,合并数据
    df.dropna(axis=0,how = 'all', inplace = True) # 去除空行
    a = np.sort(df.columns)
    b = np.sort([ '客户数', '响应数','占比(%)'])
    c = np.sort(['预测概率', '实际值'])
    
    # 判断数据输入类型 蓝底数据
    if all(x1 == x2 for (x1, x2) in zip(a, b)): # 手动测试True,但是在此处被判定为False
        # 初始化
        cn = [] # customer number 客户数
        rn = [] # response number 响应数
        rr = [] # response ratio, 响应率
        crr = [] # cumulative rr, 累计响应率
        lift = [] # 提升度

        for i in np.arange(len(df)): # 计算 响应率 和 累计响应率
            k = df.iloc[i][['客户数','响应数']] # 获取单次遍历数据
            cn.append(k[0]) # 储存数据
            rn.append(k[1]) 
            rr.append(round(k[1]/k[0],3)) 
            crr.append(round(sum(rn)/sum(cn),3)) # 保留三位小数,即百分比形势下保留一位小数

        for i in crr: # 计算 提升度
            lift.append(round(i/crr[-1],1))

        # 数据储存
        df1 = df.copy()
        df1['响应率'] = rr
        df1['累计响应率'] = crr
        df1['提升度'] = lift

    else:
        return print('\033[31m错误:文件数据有误') # error print
        
    return df1.to_excel('lift.xlsx',index = False)

Aucun commentaire:

Enregistrer un commentaire