推荐学习书目
› 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
zcsnbb
V2EX  ›  Python

pandas 如何将某一列大于 0 的数字全部置为 1

  •  
  •   zcsnbb · Sep 28, 2018 · 4842 views
    This topic created in 2919 days ago, the information mentioned may be changed or developed.

    date rtn 0 2016-01-01 0.101536 1 2016-01-02 0.055109 2 2016-01-03 -0.163625 3 2016-01-04 0.086756 4 2016-01-05 0.035907 5 2016-01-06 0.047619 6 2016-01-07 0.018717 7 2016-01-08 -0.036290

    这样子的 df

    大佬帮帮忙,实在搞不出来了

    6 replies  •  2018-09-29 15:32:59 +08:00
    neosfung
        1
    neosfung  
       Sep 28, 2018
    df['rtn'] = df['rtn'].apply(lambda x: 1 if x>0 else x)
    jswangjieda
        2
    jswangjieda  
       Sep 28, 2018
    df.loc[df['rtn']>0,'rtn']=1
    ddzzhen
        3
    ddzzhen  
       Sep 28, 2018 via Android
    直接改为 true,false 矩阵
    lieh222
        4
    lieh222  
       Sep 29, 2018
    x = x**0
    lieh222
        5
    lieh222  
       Sep 29, 2018
    @lieh222 写错,请忽略。。。
    LaFayette
        6
    LaFayette  
       Sep 29, 2018
    df['rtn'] = np.where(df['rtn']>0, 1, df['rtn'])
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2568 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 271ms · UTC 10:02 · PVG 18:02 · LAX 03:02 · JFK 06:02
    ♥ Do have faith in what you're doing.