Python新手提问:如何将上面的DataFrame转换为下面的DataFrame?
user_id r_score f_score m_score r_height
0 1 5 1 1
1 2 2 1 1
2 5 4 1 1
3 7 2 1 1
4 9 2 1 1
user_id r_score f_score m_score r_height f_height m_height
0 1 5 1 1 高 低 低
1 2 2 1 1 高 低 低
2 5 4 1 1 高 低 低
3 7 2 1 1 低 低 低
4 9 2 1 1 低 低 低
Python新手提问:如何将上面的DataFrame转换为下面的DataFrame?
map apply 看一下
判断 r_score 是否大于 df[‘rscore’]的平均值,大于输出‘高’,小与输出‘低’,生成新的一列 df['r_height ']记录下来,再同样生成 df['f_height '],df[‘m_height’],大佬有什么思路吗,复杂的自定义函数还不是很熟练
mark,晚上回去开电脑给你写,9 大于平均值为什么是 1 ? 1 小于平均值为什么是高?
df[‘r_height’] = df[‘r_score’].apply(lambda x:[‘低’,‘高’][int(x>df[‘r_score’].mean())])
df[‘f_height’] = df[‘f_score’].apply(lambda x:[‘低’,‘高’][int(x>df[‘f_score’].mean())])
df[‘m_height’] = df[‘m_score’].apply(lambda x:[‘低’,‘高’][int(x>df[‘m_score’].mean())])
这样?
写个函数,apply 直接应用在 df 上,这样应该是循环一次。


