Python 怎么把上面的 df 变成下面的 df,帮忙解答一下, Python 新手,谢谢

2018-11-11 13:37:37 +08:00
 lemeet
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 低 低 低
2480 次点击
所在节点    Python
5 条回复
wqzjk393
2018-11-11 13:50:06 +08:00
map apply 看一下
lemeet
2018-11-11 13:59:11 +08:00
@wqzjk393 判断 r_score 是否大于 df['rscore']的平均值,大于输出‘高’,小与输出‘低’,生成新的一列 df['r_height ']记录下来,再同样生成 df['f_height '],df['m_height'],大佬有什么思路吗,复杂的自定义函数还不是很熟练
princelai
2018-11-11 15:44:39 +08:00
mark,晚上回去开电脑给你写,9 大于平均值为什么是 1 ? 1 小于平均值为什么是高?
lsvih
2018-11-11 16:32:40 +08:00
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())])

这样?
chesterzzy
2018-11-12 09:39:31 +08:00
写个函数,apply 直接应用在 df 上,这样应该是循环一次。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/506672

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX