使用python3.6 TypeError:需要整数

2024-05-23 16:22:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是python 3.6版本,出现以下错误:

TypeError: an integer is required (invsf['Destn Branch'] = invsf.apply(lambda x: convloc(x['Destn Branch'])))

代码:

loclist = ['Destn Branch','Hub SC Location','Origin Branch']
maplist = dict({'MAAG': 'MAAC','NEIR': 'GAUB','RJPR': 'PTLF','SIKM': 'SILB','KLMF':'COKB','AMDE':'AMDO'})
print (loclist)
totalconsinv = len(invsf)

## Check done 
def convloc(location):
    get_dict = maplist.get(location)
    print ('get_dict',get_dict)
    if get_dict is None:
        #print 'location',location
        return location
    else:
        return get_dict


invsf['Destn Branch'] = invsf.apply(lambda x: convloc(x['Destn Branch']))

如何修复此错误?你知道吗


Tags: lambdabranchgetreturnis错误locationdict
1条回答
网友
1楼 · 发布于 2024-05-23 16:22:04

几点提示:

  • 您已经用{...}声明了一个dict。在上面调用dict()是多余的。你知道吗
  • 如果apply操作只影响一列,则应该对该序列调用apply。你知道吗

    invsf['Destn Branch'] = invsf['Destn Branch'].apply(covloc)
    

    这将允许您摆脱lambda


但是,在您的情况下,调用map更合适。你知道吗

invsf['Destn Branch'] = invsf['Destn Branch'].map(maplist)

相关问题 更多 >