1

(Very new coder, first time here, apologies if there are errors in writing)

I have a csv file I made from Excel called SouthKoreaRoads.csv and I'm supposed to read that csv file using Pandas. Below is what I used:

import pandas as pd
import os

SouthKoreaRoads = pd.read_csv("SouthKoreaRoads.csv")

I get a FileNotFoundError, and I'm really new and unsure how to approach this. Could anyone help, give advice, or anything? Many thanks in advance

Bopy
  • 23
  • 1
  • 4
  • Is the file "SouthKoreaRoads.csv" in the same directory as the python file? If not you would need to update your path accordignly. – Abdelrahman Shoman Jul 15 '21 at 07:41
  • @AbdelrahmanShoman how can I check this? – Bopy Jul 15 '21 at 07:49
  • In your code you are trying to read a file named "SouthKoreaRoads.csv". This file has to be somewhere on your computer and the same goes for your python/jupyter notebook. Are they both in the same directory (or folder) or not? ... If you are not able to tell if two files are in the same folder maybe you need some windows basics (assuming you are using windows) (https://www.youtube.com/watch?v=4xS5IOg_nDw) – Abdelrahman Shoman Jul 15 '21 at 07:53
  • Under Users > kahaa I have .conda, .ipython, .jupyter, anoconda3, and SouthKoreaRoads.csv Is that what you mean? Sorry probably a really beginer question – Bopy Jul 15 '21 at 07:55
  • do you have the absolute path for the CSV file? And the absolute path for the python file? for example something like this: `C:\Users\kahaa\SouthKoreaRoads.csv` and `C:\Users\kahaa\python_example.ipynb` . If you are still not sure, maybe check this https://www.youtube.com/watch?v=-QAED9UaMIE – Abdelrahman Shoman Jul 15 '21 at 08:07
  • C:\Users\kahaa\SouthKoreaRoads.csv is what I got too. And when I did the pwd from the video I got: C:\\Users\\kahaa How should I now find the absolute path of the python file? Thank you for the help sir – Bopy Jul 15 '21 at 08:20
  • Are you sure that th spelling (including case) is right? Go to the folder the file is in and copy the filename instead. – Serge de Gosson de Varennes Jul 15 '21 at 08:22
  • What should I do once I copy the name? @ser – Bopy Jul 15 '21 at 08:30
  • Paste it in your code. – Serge de Gosson de Varennes Jul 15 '21 at 08:33
  • I did this: import pandas as pd import os SouthKoreaRoads = pd.read_csv("SouthKoreaRoads.csv") and now when I run it nothing pops up. There is no error message, but nothing else either. – Bopy Jul 15 '21 at 08:38
  • @Bopy if no error message poped out that means the code was executed succesfully. if you want to see the contents of the variable `SouthKoreaRoads` just create a new cell, write `SouthKoreaRoads` then execute the cell or try `print(SouthKoreaRoads)` or maybe check this https://stackoverflow.com/questions/26873127/show-dataframe-as-table-in-ipython-notebook – Abdelrahman Shoman Jul 15 '21 at 09:06
  • @Bopy also keep in mind that Google is your friend. And most of these information are just one search away. If you did your research you can then ask a question and explain your problem and what you did to try and fix it – Abdelrahman Shoman Jul 15 '21 at 09:08
  • Sounds good, I will do that in the future, thank you. – Bopy Jul 15 '21 at 17:06

3 Answers3

1

just some explanation aside. Before you can use pd.read_csv to import your data, you need to locate your data in your filesystem.

Asuming you use a jupyter notebook or pyton file and the csv-file is in the same directory you are currently working in, you just can use:

import pandas as pd SouthKoreaRoads_df = pd.read_csv('SouthKoreaRoads.csv')

If the file is located in another directy, you need to specify this directory. For example if the csv is in a subdirectry (in respect to the python / jupyter you are working on) you need to add the directories name. If its in folder "data" then add data in front of the file seperated with a "/"

import pandas as pd SouthKoreaRoads_df = pd.read_csv('data/SouthKoreaRoads.csv')

Pandas accepts every valid string path and URLs, thereby you could also give a full path.

import pandas as pd SouthKoreaRoads_df = pd.read_csv('C:\Users\Ron\Desktop\Clients.csv')

so until now no OS-package needed. Pandas read_csv can also pass OS-Path-like-Objects but the use of OS is only needed if you want specify a path in a variable before accessing it or if you do complex path handling, maybe because the code you are working on needs to run in a nother environment like a webapp where the path is relative and could change if deployed differently.

please see also:

https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html https://docs.python.org/3/library/os.path.html

BR

fabigr8
  • 11
  • 1
0
SouthKoreaRoads = pd.read_csv("./SouthKoreaRoads.csv")

Try this and see whether it could help!

Leo6Leo
  • 25
  • 7
0

Try to put the full path, like "C:/users/....".

Dharman
  • 30,962
  • 25
  • 85
  • 135