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

问一个 pandas 处理数据的简单问题

  •  
  •   kangsgo · 2016-09-17 19:38:28 +08:00 · 1625 次点击
    这是一个创建于 2786 天前的主题,其中的信息可能已经有所发展或是发生改变。

    数据如下:

    //file=date.txt
    1::F::1::10::48067
    2::M::56::16::70072
    3::M::25::15::55117
    4::M::45::7::02460
    5::M::25::20::55455
    

    比如我只想要提取前 3 列的内容,我的命令如下:

    //file=volume.py
    import pandas as pd
    
    unames=['snapshot','volume','test']
    id = pd.read_table("date.txt",sep="::",header=None,names=unames,engine='python')
    test=id
    pd.concat([test]).to_csv('volume.txt',sep=" ",index=False)
    

    但是获得的是后 3 列的内容

    //file=volue.txt
    snapshot volume test
    1 10 48067
    56 16 70072
    25 15 55117
    45 7 2460
    25 20 55455
    

    我想问的是如何提取前 3 列或者制定要哪几列作为内容吗?

    3 条回复    2016-09-17 19:52:55 +08:00
    raptium
        1
    raptium  
       2016-09-17 19:44:27 +08:00 via Android   ❤️ 1
    先全读进来,然后 df.iloc[:,[:3]]
    kangsgo
        2
    kangsgo  
    OP
       2016-09-17 19:48:49 +08:00
    @raptium 非常感谢!
    ruoyu0088
        3
    ruoyu0088  
       2016-09-17 19:52:55 +08:00
    有一个 usecols 参数,使用 usecols=[0, 1, 2]即可。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2439 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 12:07 · PVG 20:07 · LAX 05:07 · JFK 08:07
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.