Python Pandas:读取文件时如何跳过列? [英] Python Pandas : How to skip columns when reading a file?

查看:1576
本文介绍了Python Pandas:读取文件时如何跳过列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表格格式如下:

foo - bar - 10 2e-5 0.0 some information
quz - baz - 4 1e-2 1 some other description in here

当我用熊猫打开它时:

a = pd.read_table("file", header=None, sep=" ")

它告诉我:

CParserError: Error tokenizing data. C error: Expected 9 fields in line 2, saw 12

我基本上想要拥有的是与skiprows选项类似的东西,它将允许我做类似的事情:

What I'd basically like to have is something similar to the skiprows option which would allow me to do something like :

a = pd.read_table("file", header=None, sep=" ", skipcolumns=[8:])

我知道我可以使用awk重新设置该表格的格式,但是我想知道是否存在Pandas解决方案.

I'm aware that I could re-format this table with awk, but I'd like to known whether a Pandas solution exists or not.

谢谢.

推荐答案

usecols参数允许您选择要使用的列:

The usecols parameter allows you to select which columns to use:

a = pd.read_table("file", header=None, sep=" ", usecols=range(8))

但是,要接受不规则的列数,还需要使用engine='python'.

However, to accept irregular column counts you need to also use engine='python'.

这篇关于Python Pandas:读取文件时如何跳过列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆