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'])
df
Is there a more elegant and more efficient approach to this?
Thanks ahead.
question from:
https://stackoverflow.com/questions/65882258/pandas-lookup-to-be-deprecated-elegant-and-efficient-alternative 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…