4

When I tried to read a csv into a dataframe in Pandas it can't find my file what are some possible solutions?

Here is the error:

*FileNotFoundError                         Traceback (most recent call last)
<ipython-input-2-0c537d0c5b39> in <module>
----> 1 data = pd.read_csv('1.01. Simple linear regression.csv')

C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\parsers.py in parser_f(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, dialect, error_bad_lines, warn_bad_lines, delim_whitespace, low_memory, memory_map, float_precision)
2
  • Please post your code, and use the formatting tools so we can see what is going on. Also, it is unclear; you mention calling Excel, but you have python tags. Are you trying to read a CSV file, or call Excel from Python? Commented Jun 13, 2020 at 23:05
  • The line of code you posted above assumes your csv file is stored in python working directory. Is it the case? You can run import os; os.getcwd() to determine Python current working directory. Commented Jun 14, 2020 at 10:15

1 Answer 1

4

Make sure you are in the correct directory

Pandas.read_csv, Python will always look in your “current working directory“

data = pd.read_csv('1.01. Simple linear regression.csv')
data.head()

You can always a give a full path

Pandas.read_csv, Python can also look in a specified folder “current working directory“

I have to do by directory path most of the time myself and set my encoding and add a r before also.

data = pd.read_csv(r'C:\Users\path\to\your\file\mess.csv', encoding='utf8')
data.head()

you can also rename the file name to keep it simple

1.01. Simple linear regression.csv 

into

1_01_Simple_linear_regression.csv


 data = pd.read_csv('C:\Users\path\to\your\file\1_01_Simple_linear_regression.csv')
 data.head()

Sometimes you also need to double \ in the path

data = pd.read_csv("C:\\Users\\path\\1_01_Simple_linear_regression.csv")
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much, the mistake was that i saved the .csv file on another folder, couple with the fact that the path wasn't well defined.
Cool, I am glad it helped. I did the same a bunch of times when I started too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.