2

I am using python 3.6 version, I am getting following error:

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

Code:

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']))

How do I fix this error?

1
  • what's invsf? Commented Nov 10, 2017 at 11:00

1 Answer 1

2

A few pointers:

  • You've declared a dict with {...}. Calling dict() over it is redundant.
  • If your apply operation affects one column only, you should call apply on that series.

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

    This would allow you to get rid of the lambda.


In your case, however, calling map would be more suitable.

invsf['Destn Branch'] = invsf['Destn Branch'].map(maplist)
Sign up to request clarification or add additional context in comments.

1 Comment

I was about to suggest passing an axis to apply but calling apply for a column is by far the best. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.