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.
2 Answers
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
Mohit Motwani
Please consider explaining your answer. Answers with no explanation are usually tagged as low quality posts for review.
df['col_name'] = 5 - df['col_name']?