简体   繁体   中英

How to import csv as a pandas dataframe?

I have a csv file which is located on my computer, the path looks like: Macintosh HD/somefolder/anotherfolder/onemorefolder/file.csv

However when I try to import that csv as a dataframe in pandas using the code:

df = pd.read_csv("/Macintosh HD/somefolder/anotherfolder/onemorefolder/file.csv", sep =";")

PyCharm returns an error claiming there is no such directory or file. OKay, I insert a direct link where the same csv is located online, in this case the code looks like:

df = pd.read_csv("https://disk.yandex.ru/d/yaPJSq26gFvxHw", sep =";")

and now PyCharm says:

urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1123)>

What's wrong in my code and why don't I manage to import that csv directly from my mac?

You can use relative path by putting the csv in the same folder with the script or notebook you are trying to read the csv.

df = pd.read_csv('file.csv', sep=';')

or in a folder that is in the same folder with the script/notebook:

df = pd.read_csv('name_of_the_folder/file.csv', sep=';')

The link you are using is not the url of the csv file, it is the url of the web page you can download the csv so it won't work.

I think this is a path problem rather than a pandas issue. Try opening the same file with the built-in open() function. To get the correct path to navigate over to the directory containing the csv file and write pwd in the terminal (for macOS). Copy this path and just append the <filename>.csv

Possible Solutions


  1. Move the file.csv to the same folder as the python script or Jupyter Notebook and then simply use pd.read_csv("file.csv", sep = ";") .

  2. The URL which you shared redirects to a page but doesn't download the csv file directly. If you have the file available in s3 or gs, try using that link.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM