Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
299 views
in Technique[技术] by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...