V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
gubo2012
V2EX  ›  Python

大家用 pandas dataframe 有什么好的跨行( row)计算的方法?

  •  
  •   gubo2012 · Aug 27, 2016 · 4776 views
    This topic created in 3539 days ago, the information mentioned may be changed or developed.
    举个简单的例子, 1,2,3 三行, A,B,C 三列,要求C(n+1) = A(n) + B(n),n 表示行数,这个有什么好的实现方法?

    有个比较笨的方法是, 1 )加一列 D ,记录行数, 2 )再加一列 E , E = D +1,3)克隆一个 df,4)将两个 df   merge ,让第一个 df 的D列等于第二个 df 的E列,然后计算计算A+B。这样可行,但太麻烦。

    请问有没有更好的方法, loop 除外。谢谢
    2 replies    2016-08-27 21:55:19 +08:00
    lkx4102
        1
    lkx4102  
       Aug 27, 2016   ❤️ 1
    df.['c'] = df.['b'].shift(1) + df.['a'].shift(1)
    zhuangzhuang1988
        2
    zhuangzhuang1988  
       Aug 27, 2016
    试试矩阵乘法。。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5737 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 59ms · UTC 08:36 · PVG 16:36 · LAX 01:36 · JFK 04:36
    ♥ Do have faith in what you're doing.