2

I have a DataFrame, df, where I would like to replace several values

user1 user2 user3
apple yoo apple
mango ram mango

Instead of doing


df['user1'] = df['user1'].replace(['apple','mango'], [0, 1])
df['user3'] = df['user1'].replace(['apple','mango'], [0, 1])
df['user2'] = df['user2'].replace(['yoo','ram'], [2, 3])


to get the final DataFrame of

user1 user2 user3
0 2 0
1 3 1

Is there any way I make the code above more efficient such that I can change the values of apple, mango, yoo and ram with one line of code?

1
  • Something like df.replace(['apple', 'mango', 'yoo', 'ram'], [0, 1, 2, 3]) works? Unless you actually need different mappings in different columns. Commented Nov 12, 2021 at 13:24

1 Answer 1

2

If need set range by unique values per columns use:

cols = ['user1','user2','user3']
s = df[cols].unstack()
df[cols] = pd.Series(pd.factorize(s)[0], index=s.index).unstack(0)
print (df)
   user1  user2  user3
0      0      2      0
1      1      3      1
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.