推荐学习书目
› 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 · 4961 views
    This topic created in 2973 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
    neosfung
    Sep 28, 2018
    1
    df['rtn'] = df['rtn'].apply(lambda x: 1 if x>0 else x)
    jswangjieda
    jswangjieda
    Sep 28, 2018
    2
    df.loc[df['rtn']>0,'rtn']=1
    ddzzhen
    ddzzhen
    Sep 28, 2018 via Android
    3
    直接改为 true,false 矩阵
    lieh222
    lieh222
    Sep 29, 2018
    4
    x = x**0
    lieh222
    lieh222
    Sep 29, 2018
    5
    @lieh222 写错,请忽略。。。
    LaFayette
    LaFayette
    Sep 29, 2018
    6
    df['rtn'] = np.where(df['rtn']>0, 1, df['rtn'])
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   950 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 19:45 · PVG 03:45 · LAX 11:45 · JFK 14:45
    ♥ Do have faith in what you're doing.