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

dataframe 对含中文的字符串按字符位切片

  •  
  •   yeyu1989 · Feb 26, 2018 · 4691 views
    This topic created in 3141 days ago, the information mentioned may be changed or developed.
    需要对 dataframe 中的字符串数据切片,字符串按字符位切片(一个中文占 2 个字符)。
    举例:
    columnA
    I'm 中国, so
    You are 中国人
    ...

    取 columnA 每个字符串的第 9 位到第 10 位,则结果应为
    ,s
    中

    假设不存在中文字符被分割的情况,应该怎么写?

    我理解的方式是:
    df.str.decode('gb18030').str.slice(8,10).str.encode('gb18030')

    但是 decode 之后的 Series 就不是 str 了,后面调用的 str.slice 等就会报错
    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   751 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 21:00 · PVG 05:00 · LAX 14:00 · JFK 17:00
    ♥ Do have faith in what you're doing.