【发布时间】:2016-03-29 08:04:05
【问题描述】:
所有,我有一个 190 列和 902 行的分析 csv 文件。我需要重新编码几列(准确地说是 18 个)中的值,从当前的 1-5 Likert 缩放到 0-4 Likert 缩放。
我尝试过使用替换:
df.replace({'Job_Performance1': {1:0, 2:1, 3:2, 4:3, 5:4}}, inplace=True)
但这会引发值错误:“不允许使用重叠的键和值进行替换”
我可以使用地图:
df['job_perf1'] = df.Job_Performance1.map({1:0, 2:1, 3:2, 4:3, 5:4})
但是,我知道必须有一种更有效的方法来实现这一点,因为这个用例是统计分析和统计软件中的标准,例如SPSS
我在 StackOverFlow 上查看了多个问题,但没有一个完全适合我的用例。 例如Pandas - replacing column values、pandas replace multiple values one column、Python pandas: replace values multiple columns matching multiple columns from another dataframe
建议?
【问题讨论】:
-
从列中减去
1有什么问题? -
我觉得这个问题没有得到正确的回答。如果您不能只使用
-1,例如因为您想要替换{1:0, 2:1, 4:3},该怎么办?