python NameError:未定义名称“res”

l3zydbqr  于 2023-06-04  发布在  Python
关注(0)|答案(2)|浏览(436)

我得到如下错误:

名称错误Traceback(most recent call last)in 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)
fhity93d

fhity93d1#

最后一行代码write_excel(res)引用了未在代码中定义的变量res
错误消息NameError: name 'res' is not defined应该可以为您提供一些线索,以了解发生了什么

wqnecbli

wqnecbli2#

你首先需要初始化变量'res'。你可以通过设置res = 0..

相关问题