Home > Backend Development > Python Tutorial > How to Read CSV Files from URLs with Pandas?

How to Read CSV Files from URLs with Pandas?

DDD
Release: 2024-11-03 01:31:29
Original
280 people have browsed it

How to Read CSV Files from URLs with Pandas?

Reading CSV Files from URLs with Pandas

Reading CSV files directly from URLs is a common task in data analysis. However, when using Pandas with certain versions, attempting to read a CSV file from a URL using the read_csv() method can result in an error like:

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

This error occurs because the content retrieved from the URL is of type 'bytes', while read_csv() expects a file path or file-like object.

Solution for Pandas Version 0.19.2 and Up

In Pandas versions 0.19.2 and above, a convenient solution is available:

<code class="python">import pandas as pd

url = "https://raw.githubusercontent.com/cs109/2014_data/master/countries.csv"
c = pd.read_csv(url)</code>
Copy after login

Simply pass the URL directly to read_csv(), and Pandas will handle the retrieval and parsing of the CSV file automatically.

The above is the detailed content of How to Read CSV Files from URLs with Pandas?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template