推荐学习书目
› 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 · Nov 11, 2018 · 3200 views
    This topic created in 2883 days ago, the information mentioned may be changed or developed.
    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 replies  •  2018-11-12 09:39:31 +08:00
    wqzjk393
        1
    wqzjk393  
       Nov 11, 2018 via iPhone
    map apply 看一下
    lemeet
        2
    lemeet  
    OP
       Nov 11, 2018
    @wqzjk393 判断 r_score 是否大于 df['rscore']的平均值,大于输出‘高’,小与输出‘低’,生成新的一列 df['r_height ']记录下来,再同样生成 df['f_height '],df['m_height'],大佬有什么思路吗,复杂的自定义函数还不是很熟练
    princelai
        3
    princelai  
       Nov 11, 2018 via Android
    mark,晚上回去开电脑给你写,9 大于平均值为什么是 1 ? 1 小于平均值为什么是高?
    lsvih
        4
    lsvih  
       Nov 11, 2018
    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  
       Nov 12, 2018
    写个函数,apply 直接应用在 df 上,这样应该是循环一次。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   920 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 116ms · UTC 18:07 · PVG 02:07 · LAX 11:07 · JFK 14:07
    ♥ Do have faith in what you're doing.