2

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.

1
  • it works for the csv example in your post Commented May 23, 2015 at 12:33

2 Answers 2

3

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)
Sign up to request clarification or add additional context in comments.

3 Comments

Wajdi, I tried that and I get a different error: CParserError: Error tokenizing data. C error: Calling read(nbytes) on source failed. Try engine='python'. Using the engine = 'python' parameter just spits out another error UTF-16 stream does not start with BOM'.
hmmmm... what version of pandas you have? @BabyJ
0.15.2 @Wajdi Farhani
3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.