Search code examples
pythonpandascsvtokenize

Pandas, ParserError: Error tokenizing data


I'm studying the book "Python for Data Analysis (written by Wes McKinney)."

While working on CH10, I had a problem.

The code below is what I wrote in Google Colab.

!wget https://github.com/wesm/pydata-book/blob/2nd-edition/examples/stock_px_2.csv

close_px = pd.read_csv('stock_px_2.csv', parse_dates=True, index_col=0)

close_px.head()

Then I got this error.

ParserError: Error tokenizing data. C error: Expected 1 fields in line 107, saw 2

How can I fix this?

Thanks!


Solution

  • Using the raw CSV file, I can read the values:

    import pandas as pd
    
    csv_url = "https://raw.githubusercontent.com/wesm/pydata-book/2nd-edition/examples/stock_px_2.csv"
    close_px = pd.read_csv(csv_url, parse_dates=True, index_col=0)
    print(close_px.head())
    

    Output:

                AAPL   MSFT    XOM     SPX
    2003-01-02  7.40  21.11  29.22  909.03
    2003-01-03  7.45  21.14  29.24  908.59
    2003-01-06  7.45  21.52  29.96  929.01
    2003-01-07  7.43  21.93  28.95  922.93
    2003-01-08  7.28  21.31  28.83  909.93