首页 > 解决方案 > 空列:对象是 'float' 并且没有 len()

问题描述

Bu运行此代码我试图过滤它读取的文件,其中一个过滤器是:

如果文件有 'SOURCE' 列,则:如果 'SOURCE' 列的 lambda len().max() > 0,我们可以断定它不为空,然后按照代码...

但!!如果'SOURCE'列是空的,它告诉我这是一个'float',因此它没有错误形式的长度。我只是想过滤:如果'SOURCE'列是空的,不是逐行而是整列,然后这样做,否则这样做......

for f in files_xlsx:
wb = load_workbook(input_path + "\\" + f, read_only=True)
if 'New Leads' in wb.sheetnames:
    df = pd.read_excel(input_path + "\\" + f, sheet_name="New Leads")
    colre = [col for col in df.columns]
    for eo in colre:
        df.rename(columns={eo: eo.replace('*','').replace('**','') for eo in colre})
    dtcol = [col for col in df.columns if "Date" in col]
    for ex in dtcol:
        df.rename(columns={ex: "Activity Date"}, inplace=True)
    cols = df.columns
    if "SOURCE" in cols:
        if df.dtypes['SOURCE'] != 'float':
            if df.SOURCE.map(lambda x: len(x)).max() > 0:
                df.dropna(how = 'all')
                df['File'] = f
                df_xlsx = pd.concat([df, df_xlsx], ignore_index=True)
                df_xlsx = df_xlsx[["Email","SOURCE","File","Activity Date"]]
            else:
                df_ns = df_ns.append([f], ignore_index=True)
        else:
            df_ns = df_ns.append([f], ignore_index=True)
    else:
        df_ns = df_ns.append([f], ignore_index=True)
else:
    df_ns = df_ns.append([f], ignore_index=True)

标签: pythonmaxlength

解决方案


推荐阅读