Pandas read_csv from url


I"m trying to read a csv-file from given URL, using Python 3.x:

import pandas as pd
import requests

url = ""
s = requests.get(url).content
c = pd.read_csv(s)

I have the following error

"Expected file path name or file-like object, got <class "bytes"> type"

How can I fix this? I"m using Python 3.4

Answer rating: 274

In the latest version of pandas (0.19.2) you can directly pass the url

import pandas as pd


Answer rating: 219

UPDATE: From pandas 0.19.2 you can now just pass read_csv() the url directly, although that will fail if it requires authentication.

For older pandas versions, or if you need authentication, or for any other HTTP-fault-tolerant reason:

Use pandas.read_csv with a file-like object as the first argument.

  • If you want to read the csv from a string, you can use io.StringIO.

  • For the URL, you get html response, not raw csv; you should use the url given by the Raw link in the github page for getting raw csv response , which is


import pandas as pd
import io
import requests


in Python 2.x, the string-buffer object was StringIO.StringIO

Get Solution for free from DataCamp guru