NameError:名称“ res”未定义

问题描述

我收到以下错误

NameError跟踪(最近一次通话) 在 81 writer.save() 82 ---> 83个write_excel(res) 84

NameError:名称'res'未定义

能请你帮忙吗? :)

def read_merge(file):
    """" read files and merge the data """
    
    username = pd.read_csv('file.csv',delimiter = ';')
    merged_output = pd.read_excel('file-1.xlsx')
    merged_output = pd.merge(merged_output,username,how='left',left_on=['h'],right_on=['h2'])
    merged_output = merged_output.drop(columns=['h2'])
    return(merged_output)


def remove_string(merged_output):
    """" remove rows containing strings from list_string """
    
    list_string = ["obj-1","obj-2"]
    res = merged_output[~merged_output['Rule'].isin(list_string)]
    return res


def add_agg_columns(res):
    """"aggregate columns"""
    
    res = res.groupby('Entity_Name').agg({'Rule': ','.join,'Name':'first','Area': 'first','Summary': 'first'}).reset_index()
    
                     
def write_excel(res):
    """"Write Excel document"""
    
    writer = pd.ExcelWriter(r"C:\PATH\output.xlsx",engine = 'xlsxwriter')  
    workbook=writer.book
    worksheet=workbook.add_worksheet('SHEET')
    writer.sheets['SHEET'] = worksheet
    res.to_excel(writer,sheet_name='SHEET',startcol = 0,startrow = 0)
    
    
    writer.save()
    
write_excel(res)

解决方法

代码write_excel(res)的最后一行引用了代码中未定义的变量res

错误消息NameError: name 'res' is not defined应该为您提供正在发生的事情的线索