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

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

  •  
  •   yeyu1989 · 2018-02-26 15:55:48 +08:00 · 3800 次点击
    这是一个创建于 2222 天前的主题,其中的信息可能已经有所发展或是发生改变。
    需要对 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 等就会报错
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1191 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 23:19 · PVG 07:19 · LAX 16:19 · JFK 19:19
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.