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
zcsnbb
V2EX  ›  Python

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

  •  
  •   zcsnbb · 2018-09-28 20:50:54 +08:00 · 3666 次点击
    这是一个创建于 2008 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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 条回复    2018-09-29 15:32:59 +08:00
    neosfung
        1
    neosfung  
       2018-09-28 21:12:13 +08:00
    df['rtn'] = df['rtn'].apply(lambda x: 1 if x>0 else x)
    jswangjieda
        2
    jswangjieda  
       2018-09-28 21:40:14 +08:00
    df.loc[df['rtn']>0,'rtn']=1
    ddzzhen
        3
    ddzzhen  
       2018-09-28 22:43:41 +08:00 via Android
    直接改为 true,false 矩阵
    lieh222
        4
    lieh222  
       2018-09-29 08:38:47 +08:00
    x = x**0
    lieh222
        5
    lieh222  
       2018-09-29 08:39:22 +08:00
    @lieh222 写错,请忽略。。。
    qianc1990
        6
    qianc1990  
       2018-09-29 15:32:59 +08:00
    df['rtn'] = np.where(df['rtn']>0, 1, df['rtn'])
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1382 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 17:37 · PVG 01:37 · LAX 10:37 · JFK 13:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.