使用python3.6 TypeError:需要一个整数

时间:2017-11-10 10:34:11

标签: python pandas

我正在使用python 3.6版本,我收到以下错误:

  

TypeError:需要一个整数(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']))

如何解决此错误?

1 个答案:

答案 0 :(得分:2)

一些指示:

  • 您已使用{...}声明了一个词典。在dict()上面调用它是多余的。
  • 如果您的应用操作仅影响一列,则应该对该系列调用apply。

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

    这将允许您摆脱lambda

但是,在您的情况下,调用map会更合适。

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