JakobJ
JakobJ

Reputation: 51

Error when using skiplines when using read_csv in Pandas

I am trying to import a dataframe into Python using read_csv from the Pandas library. The top of the data file (annual_precip.csv) looks like this:

Average annual precipitation and land area ,,
,mm/year,thousand km^2
country,precip,area
Afghanistan,327,652.2
Albania,1485,27.4
Algeria,89,2381.7
American Samoa,,0.2

Here is my code:

from pandas import read_csv
read_csv('annual_precip.csv', index_col = [0], skiprows = 2)

This produces the following error:

Traceback (most recent call last):`

File "<ipython-input-894-742b462476f6>", line 1, in <module>
rain =read_csv('exploratory_computing_with_python/notebook5/annual_precip.csv', skiprows = 2, index_col = [0])`

File "/Users/jakoberickson/anaconda/lib/python2.7/site-packages/pandas/io/parsers.py", line 465, in parser_f
return _read(filepath_or_buffer, kwds)`

File "/Users/jakoberickson/anaconda/lib/python2.7/site-packages/pandas/io/parsers.py", line 241, in _read
parser = TextFileReader(filepath_or_buffer, **kwds)`

File "/Users/jakoberickson/anaconda/lib/python2.7/site-packages/pandas/io/parsers.py", line 557, in __init__
self._make_engine(self.engine)`

File "/Users/jakoberickson/anaconda/lib/python2.7/site-packages/pandas/io/parsers.py", line 694, in _make_engine
self._engine = CParserWrapper(self.f, **self.options)`

File "/Users/jakoberickson/anaconda/lib/python2.7/site-packages/pandas/io/parsers.py", line 1061, in __init__
self._reader = _parser.TextReader(src, **kwds)`

File "pandas/parser.pyx", line 512, in pandas.parser.TextReader.__cinit__ (pandas/parser.c:4804)`

ValueError: No columns to parse from file`

I get no error when I skip 1 or 0 lines but then the names of my columns are incorrect. I am running Spyder 2.3.4 in the Anaconda package on Yosemite if that make any difference.

Upvotes: 2

Views: 988

Answers (2)

JakobJ
JakobJ

Reputation: 51

I updated my pandas to '0.16.1' and now I am having no problems. Thanks for you help @Wajdi Farhani

Upvotes: 3

farhawa
farhawa

Reputation: 10388

It works for the csv example in your example but your problem is that your csv file contain non-printable characters.

Try this:

df = pd.read_csv('annual_precip.csv', index_col = [0],  encoding='utf-8', skiprows = 2)

Upvotes: 3

Related Questions