Skip to main content
 首页 » 编程设计

python之使用python3.6 TypeError : an integer is required

2024年10月01日36dyllove98

我正在使用 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'])) 

如何修复此错误?

请您参考如下方法:

一些提示:

  • 您已使用 {...} 声明了一个字典。对其调用 dict() 是多余的。
  • 如果您的应用操作只影响一列,您应该对该系列调用应用。

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

    这将使您摆脱 lambda。


然而,在您的情况下,调用 map 会更合适。

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