Pandas can read CSV files directly from a URL by passing the URL to the read_csv() method. This is useful when working with datasets hosted online and for ad hoc tests.

We can use the following syntax to read CSV from URL in Pandas:

(1) Margin only on rows

import pandas as pd
url = "https://raw.githubusercontent.com/softhints/Pandas-Exercises-Projects/refs/heads/main/data/europe_pop.csv"
df = pd.read_csv(url)

Basic Usage

You can use pd.read_csv() with a URL just like you would with a local file:

import pandas as pd
url = "https://raw.githubusercontent.com/softhints/Pandas-Exercises-Projects/refs/heads/main/data/europe_pop.csv"
df = pd.read_csv(url)

This will download and read the CSV file into a DataFrame.

Notes

  • The URL must point directly to a .csv file
  • Works with:
    • http
    • https
    • ftp
  • If the CSV is encoded differently (e.g. UTF-16), you can specify it as param:
df = pd.read_csv(url, encoding='utf-16')

Read Data with Requests

In some cases you may need to use Python library requests to read the data first and then load it as DataFrame. This could be related to authentication or security. In this case we can use the following code to read the data:

import pandas as pd
import io
import requests

url = "https://raw.githubusercontent.com/softhints/Pandas-Exercises-Projects/refs/heads/main/data/europe_pop.csv"

content = requests.get(url).content
df = pd.read_csv(io.StringIO(content.decode('utf-8')))
df

This approach is ideal for:

  • quick prototyping
  • accessing public datasets
  • integration with APIs or
  • GitHub-hosted data.

Further Reading