Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Pandas : How to skip columns when reading a file?

Tags:

I have table formatted as follow :

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

When I open it with pandas doing :

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

It tells me :

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

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:]) 

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.

Thanks.

like image 803
jrjc Avatar asked Jun 23 '14 12:06

jrjc


People also ask

How do I skip columns in pandas?

We can exclude one column from the pandas dataframe by using the loc function. This function removes the column based on the location. Parameters: dataframe: is the input dataframe.

How do you skip columns in pandas read Excel?

Skip Columns From Excel Sheet Sometimes while reading an excel sheet into pandas DataFrame you may need to skip columns, you can do this by using usecols param. This takes values {int, str, list-like, or callable default None}. To specify the list of column names or positions use a list of strings or a list of int.


1 Answers

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

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

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

like image 90
otus Avatar answered Oct 21 '22 14:10

otus