首页 > 解决方案 > Obscure KeyError when using groupBy even when key is present

问题描述

I read a CSV from a file, create a trucksAvl df, then merge it with an existing truckMstr df and finally do a groupby on the resulting column.

path=os.getcwd()+'/inputfiles/'+session.get('truckAvlFile')
        trucksAvl=pd.read_csv(path)
        #truck avl list has no detailsm, get from truck mstr
        trucksAvl=trucksAvl.merge(truckMstr.drop_duplicates('Type',keep='last'),on='Type',how='left')
        #both has same name, rename old
        trucksAvl.rename(columns={'Destination_x':'Destination'},inplace=True)
        groupTA=trucksAvl.groupby(('Destination','Type','truckWt','truckVol','Density')).agg({'Count':sum})
        groupTA['Used']=0

This worked perfectly finely on my Mac with Python 3.6.9

But when I moved the code to Ubuntu it kept crashing with a key error

标签: pythonpandasmacosubuntu

解决方案


I tried the following:

  1. Print column names
  2. Give an exit path if any of the necessary column names where missing.
  3. Add an encoding option to the file I was reading
        path=os.getcwd()+'/inputfiles/'+session.get('truckAvlFile')
        trucksAvl=pd.read_csv(path,encoding='utf-8-sig')
        #truck avl list has no detailsm, get from truck mstr
        trucksAvl=trucksAvl.merge(truckMstr.drop_duplicates('Type',keep='last')[['Type', 'truckWt', 'truckVol','Density','Destination']], on='Type',how='left')
        #both has same name, rename old
        trucksAvl.rename(columns={'Destination_x':'Destination'},inplace=True)
        print(truckMstr.columns, 'from  Debug to ', trucksAvl.columns)
        if ('truckWt' in trucksAvl.columns):
            if ('truckVol' in trucksAvl.columns):

Final fix was changing group by parentheses from (( ..... )) to ([....])!
Why does Mac allow this and not Ubuntu? I have no idea

groupTA=trucksAvl.groupby(('Destination','Type','truckWt','truckVol','Density')).agg({'Count':sum})
        groupTA['Used']=0
        groupTA.reset_index(level=groupTA.index.names,inplace=True)


推荐阅读