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

求助一个 pandas 问题,使用布尔筛选 DataFrame 后,怎么对某一列赋值生效

  •  
  •   jiang123574 · 2019-08-13 11:23:33 +08:00 · 2131 次点击
    这是一个创建于 1689 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Snipaste_2019-08-13_11-21-06.png

    2 条回复    2019-08-13 15:18:13 +08:00
    zhusimaji
        1
    zhusimaji  
       2019-08-13 14:13:34 +08:00
    import pandas as pd

    import numpy as np
    dates=pd.date_range('20130201',periods=6)

    a=pd.DataFrame(np.arange(24).reshape((6,4)),index=dates,columns=['a','b','c','d'])
    print(a)
    a.loc[a['a']==12,'b']=5
    print(a)
    ---------------------------------------------------------------------
    a b c d
    2013-02-01 0 1 2 3
    2013-02-02 4 5 6 7
    2013-02-03 8 9 10 11
    2013-02-04 12 13 14 15
    2013-02-05 16 17 18 19
    2013-02-06 20 21 22 23
    a b c d
    2013-02-01 0 1 2 3
    2013-02-02 4 5 6 7
    2013-02-03 8 9 10 11
    2013-02-04 12 5 14 15
    2013-02-05 16 17 18 19
    2013-02-06 20 21 22 23
    jiang123574
        2
    jiang123574  
    OP
       2019-08-13 15:18:13 +08:00
    @zhusimaji 感谢大佬
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5492 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 08:14 · PVG 16:14 · LAX 01:14 · JFK 04:14
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.