Why Jupyter can't read csv files when they are in folders? - python

today I struggle to make Jupyter read a csv file I have downloaded. When it was saved like this "C:\Users\argir\state.csv" Jupyter was able to read it. (photo: https://ibb.co/qYLmWb0 ). But when I put it in a folder and the path was "C:\Users\argir\datasets_dika_mou\state.csv" (photo: https://ibb.co/mBVQS3f ) or when it was saved in another folder on the desktop and the path was C:\Users\argir\Υπολογιστής\datasets from 50 practical-statistics-for-data-scientistsebook oreilly\state.csv" (photo: https://ibb.co/M7m3PTF ) the Jupyter couldn't read it
FileNotFoundError: [Errno 2] No such file or directory: 'state.csv'
Why does this happen?
import pandas as pd
state_data = pd.read_csv("state.csv")

From the code you put into the comment, it could be that you simply not defining the path to the file.
When your file is at the same directory as the Notebook your code will work, but when the file is not at the same path you have to tell python where to look.
like:
import pandas as pd
state_data = pd.read_csv("datasets_dika_mou/state.csv")

Related

Getting '\u202a' in front of my file path when looking for a csv file

I am currently using jupyter notebooks for a personal project and im trying to read a CSV file. I have the file path and it works in pycharm, but when i am in jupyter it adds a unicode "\u202a" in front of the filepath and it throws an exception "FileNotFoundError".
Here is my code snippet
import pandas as pd
data = pd.read_csv('‪T10Y2Y.csv')
print(data)

Loading CSV into Pandas - no file directory

I need help debugging this code.
I am trying to add a csv file to my pandas data frame.
import pandas as pd
df = pd.read_csv ('batting.csv')
print(df)
When I execute this code I am getting this error:
FileNotFoundError: [Errno 2] No such file or directory: 'batting.csv'
I then tried to change the directory using os
os.getcwd()
os.chdir(r"C:\Users\crack\Downloads\excel\batting.csv")
I am now coming across this error:
NotADirectoryError: [WinError 267] The directory name is invalid: 'C:\\Users\\crack\\Downloads\\excel\\batting.csv'
I am new to coding and have been looking for a solution to this error all day.
You could try ,
df = pd.read_csv(r"C:\Users\crack\Downloads\excel\batting.csv")
instead of
df = pd.read_csv ('batting.csv')
You are on the right track. The working directory is probably not where your file is located.
Try doing the following to see where it is:
print(os.getcwd())
The error you are seeing using os.chdir() is because you have specified a filename not a directory.
You have a few possible solutions:
Specify the full path to your CSV file:
pd.read_csv(r"C:\Users\crack\Downloads\excel\batting.csv")
Change the working directory to the same folder:
os.chdir(r"C:\Users\crack\Downloads\excel")
pd.read_csv("batting.csv")
If the script and CSV files are in the same folder and you don't want to specify a fixed path:
os.chdir(os.path.dirname(os.path.abspath(__file__)))
pd.read_csv("batting.csv")
This changes the working directory to be where the script is located. It takes the full script name and uses just the directory part.

Python: Reading csv file from relative path does not work in Mac

I have few csv files in a folder which is present in the same folder where my program is stored. I am using following code to read the csv files:
import pandas as pd
import os
files = os.listdir('../review_data')
for file in files:
os.chdir('../review_data')
temp = pd.read_csv('../review_data/'+file,header=None)
print(temp)
However, when I ran the above code, the print statement prints nothing. When I ran the same code (with some modification in file path ) in windows it works fine. However, it did not work in MAC. Could anyone help me where I am making the mistake?

how to open excel file in python using pandas?

How do you open an xls or csv file in python without having to connect the whole path?
ex: instead of using c:/user/...filename how do you connect it with just filename?
is it possible using pandas? This is in order to transfer the code from on console to another and the code being able to open with ease. From my understanding, if I use the path and send the code to another computer the excel page won't open there. btw the code will be sent with the original excel sheet
In this case, I believe you would have to set your working directory to the absolute path of your .py file.
Note, for the code below, your .csv file should be in the same directory as your .py file.
import os.path
import pandas as pd
base_dir = os.path.dirname(os.path.abspath(__file__)) # set directory to location of .py file
os.chdir(base_dir) # change directory
csv_file = pd.read_csv('file.csv',sep=',') # read .csv
Similar to solution of #Ira H., but instead of changing working directory you can generate full path:
import os.path
import pandas as pd
base_dir = os.path.dirname(
os.path.abspath(__file__)
) # set directory to location of .py file
csv_file = pd.read_csv(f"{base_dir}\\full_paths.csv", sep=",") # read .csv

Relative path of excel files, python

I am trying to access an Excel file in Python, but I'm getting the following error:
FileNotFoundError: [Errno 2] No such file or directory: 'input/sales-feb-2014.xlsx'
Could you please help me defining the path of the file?
Python Code:
from xlrd import open_workbook
sheet = open_workbook('input/sales-feb-2014.xlsx').sheet_by_index(0)
print (sheet.cell_value(3,0))
You either need to add the full path to open_workbook(your_full_path_comes_here) or change the directory you are working in before.
You can use the osmodule to before calling your variable.
import os
os.chdir(r'...your/path/input')

Categories

Resources