0

is there a way I can invert values in a dataframe using Pandas? I am using data representing customers feedback. It is captured as a value from 1 to 5, 5 being the best feedback and 1 the lowest. I would like to invert it so that 1 becomes the highest score, so basically all the 5s become 1s, 4 becomes 2 and 3 stays 3. Is there a way I can achieve that? Many thanks in advance.

1
  • df['col_name'] = 5 - df['col_name']? Commented Apr 29, 2019 at 12:11

2 Answers 2

3
df['Feedback'] =6- df['Feedback'] 
Sign up to request clarification or add additional context in comments.

Comments

0

The map function is used for substituting each value in a Series with another value. So, you should try:

df['col_name'] = df['col_name'].map({5:1, 4:2, ...})

1 Comment

Please consider explaining your answer. Answers with no explanation are usually tagged as low quality posts for review.

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.