以更 effective/Pythonic 的方式调整一组行的数字比例?

Adjust numeric scale for a set of rows in a more effective/Pythonic way?

所以我的数据集是调查数据,其中每一行显示一个问题和一个唯一的受访者对该问题的数字回答。不幸的是,一些问题的比例倒退了(也就是 1s 应该是 4s 而 4s 应该是 1s)。

我想到了通过 loc 解决这个问题的最荒谬的方法。该代码首先搜索问题,然后搜索数字的实例,然后将其转换。第一个数字被转换为占位符值(例如,我没有立即将 1 转换为 4,而是将其转换为“4a”)以确保 4 不会被错误转换(等等)。在所有这些之后,它会通过并适当地转换这些占位符。

df.loc[((df['question'].str.contains('Why did the chicken cross the road?')) & (df['numericValue'] == 1)),'numericValue'] = '4a'
df.loc[((df['question'].str.contains('Why did the chicken cross the road?')) & (df['numericValue'] == 2)),'numericValue'] = '3a'
df.loc[((df['question'].str.contains('Why did the chicken cross the road?')) & (df['numericValue'] == 3)),'numericValue'] = '2a'
df.loc[((df['question'].str.contains('Why did the chicken cross the road?')) & (df['numericValue'] == 4)),'numericValue'] = 1
df.loc[((df['question'].str.contains('Why did the chicken cross the road?')) & (df['numericValue'] == '2a')),'numericValue'] = 2
df.loc[((df['question'].str.contains('Why did the chicken cross the road?')) & (df['numericValue'] == '3a')),'numericValue'] = 3
df.loc[((df['question'].str.contains('Why did the chicken cross the road?')) & (df['numericValue'] == '4a')),'numericValue'] = 4

最终,1变成4,2变成3,3变成2,4变成1。但由于这并不是一种真正有效的方法,想知道您是否有更好的主意?非常感谢!

将每个值 x 替换为 5-x

df.loc[<condition>, <column>] = 5 - df.loc[<condition>, <column>]

“条件”是您在“鸡”问题上的过滤器。


我梦想生活在一个鸡可以过马路的世界里, 没有人质疑她的动机。

如果你的替换不能算术表达,你也可以映射正确的值:

replacement = {1: 4, 2: 3, 3: 2, 4: 1}
df.loc[selector, 'numericValue'] = df.loc[selector, 'numericValue'].map(replacement)