2017-11-10 10 views
2

を必要とする、私は次のエラーを取得しています:はpython3.6にはTypeErrorを使用:整数は私の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'])) 

私はこのエラーをどのように修正すればよいですか?

+0

「invsf」は何ですか? –

+0

あなたの質問に答えがあった(私はかなり確信していました)、[回答を受け入れてください](https://stackoverflow.com/help/someone-answers)してください。 –

答えて

2

いくつかのポインタ:

  • あなたは{...}と辞書を宣言しました。その上にdict()を呼び出すことは冗長です。
  • 適用操作が1つの列にのみ影響する場合は、その系列で適用を呼び出す必要があります。

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

    これにより、lambdaを取り除くことができます。あなたのケースでは


、しかし、mapを呼び出すと、より適切であろう。

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

私は軸を渡して適用することを提案しようとしていましたが、列の適用を呼び出すことはずっと最高です。 :) – Dark

関連する問題