V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
lemeet
V2EX  ›  Python

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

  •  
  •   lemeet · 2018-11-11 13:37:37 +08:00 · 2467 次点击
    这是一个创建于 1964 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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 低 低 低
    5 条回复    2018-11-12 09:39:31 +08:00
    wqzjk393
        1
    wqzjk393  
       2018-11-11 13:50:06 +08:00 via iPhone
    map apply 看一下
    lemeet
        2
    lemeet  
    OP
       2018-11-11 13:59:11 +08:00
    @wqzjk393 判断 r_score 是否大于 df['rscore']的平均值,大于输出‘高’,小与输出‘低’,生成新的一列 df['r_height ']记录下来,再同样生成 df['f_height '],df['m_height'],大佬有什么思路吗,复杂的自定义函数还不是很熟练
    princelai
        3
    princelai  
       2018-11-11 15:44:39 +08:00 via Android
    mark,晚上回去开电脑给你写,9 大于平均值为什么是 1 ? 1 小于平均值为什么是高?
    lsvih
        4
    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
        5
    chesterzzy  
       2018-11-12 09:39:31 +08:00
    写个函数,apply 直接应用在 df 上,这样应该是循环一次。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5107 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 09:45 · PVG 17:45 · LAX 02:45 · JFK 05:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.