Pandas/python: Replace Multiple Values In Multiple Columns
All, I have an analytical csv file with 190 columns and 902 rows. I need to recode values in several columns (18 to be exact) from it's current 1-5 Likert scaling to 0-4 Likert sca
Solution 1:
You can simply subtract a scalar value from your column which is in effect what you're doing here:
df['job_perf1'] = df['job_perf1'] - 1
Also as you need to do this on 18 cols, then I'd construct a list of the 18 column names and just subtract 1
from all of them at once:
df[col_list] = df[col_list] - 1
Solution 2:
No need for a mapping. This can be done as a vector addition, since effectively, what you're doing, is subtracting 1
from each value. This works elegantly:
df['job_perf1'] = df['Job_Performance1'] - numpy.ones(len(df['Job_Performance1']))
Or, without numpy
:
df['job_perf1'] = df['Job_Performance1'] - [1] * len(df['Job_Performance1'])
Post a Comment for "Pandas/python: Replace Multiple Values In Multiple Columns"