Home > Backend Development > Python Tutorial > How Do You Read CSV Files From URLs Using Pandas?

How Do You Read CSV Files From URLs Using Pandas?

DDD
Release: 2024-11-03 17:49:30
Original
799 people have browsed it

How Do You Read CSV Files From URLs Using Pandas?

Reading CSV Files from URLs Using Pandas

When attempting to read a CSV file from a URL using Python 3.x, users may encounter the error: "Expected file path name or file-like object, got type."

This occurs because Pandas expects a file path or an open file object, while the code provides a byte-string s obtained from the URL. To resolve this issue, follow the updated Pandas implementation:

In Pandas versions later than 0.19.2, you can directly pass the URL to the read_csv() function:

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

# Replace the s variable with the direct URL
url = "https://raw.githubusercontent.com/cs109/2014_data/master/countries.csv"
c = pd.read_csv(url)</code>
Copy after login

This updated code directly reads the CSV data from the specified URL without the need for intermediate steps involving requests.get().content.

The above is the detailed content of How Do You Read CSV Files From URLs Using 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