r/pythonhelp • • May 07 '22

HOMEWORK Loading csv into pandas

Hi there, 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 and I am coming across this error:

os.getcwd()
os.chdir(r"C:\Users\crack\Downloads\excel\batting.csv")

NotADirectoryError: [WinError 267] The directory name is invalid: 'C:\\Users\\crack\\Downloads\\excel\\batting.csv'

I have been googling all day trying to fix this

3 Upvotes

2 comments sorted by

1

u/MT1961 May 07 '22

chdir requires a DIRECTORY not a file name. Remove the \batting.csv from your chdir call, and then (assuming the file is in that directory) it should work.

1

u/CraigAT May 07 '22

You could try putting the full path to the file in the read_csv line.