Search code examples
pythonpandasdataframelookup

Pandas Lookup to be deprecated - elegant and efficient alternative


The Pandas lookup function is to be deprecated in a future version. As suggested by the warning, it is recommended to use .melt and .loc as an alternative.

df = pd.DataFrame({'B': ['X', 'X' , 'Y', 'X', 'Y', 'Y', 
                         'X', 'X', 'Y', 'Y', 'X', 'Y'],
                   'group': ["IT", "IT", "IT", "MV", "MV", "MV", 
                             "IT", "MV", "MV", "IT", "IT", "MV"]})

a = (pd.concat([df, df['B'].str.get_dummies()], axis=1)
     .groupby('group').rolling(3, min_periods=1).sum()
     .sort_index(level=1).reset_index(drop=True))        

df['count'] = a.lookup(df.index, df['B'])

>  Output Warning:  <ipython-input-16-e5b517460c82>:7: FutureWarning:
> The 'lookup' method is deprecated and will be  removed in a future
> version. You can use DataFrame.melt and DataFrame.loc as a substitute.

However, the alternative appears to be less elegant and slower:

b = pd.melt(a, value_vars=a.columns, var_name='B', ignore_index=False)
b.index.name='index'
df.index.name='index'
df = df.merge(b, on=['index','B'])

Is there a more elegant and more efficient approach to this?


Solution

  • It looks like, you can just use the index to assign new values.

    dfn = df.set_index('B', append=True)
    dfn['count'] = a.stack()