微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

NameError:名称“ res”未定义

如何解决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应该为您提供正在发生的事情的线索

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。