Why this code works but this code does not?

Introduction to Pandas > Section 5 “Loading and Saving CSVs”

I am using Python 3 and Pycharm

I was able to complete Section 5. I tried to do the exact same thing I learned in Section 5 on my own in Pycharm. I did some research, and I found a solution to reading the CSV (that is posted below). Why did the code in Section 5 not work?

THIS WORKED This WAS NOT the code used to solve Section 5.

import pandas as pd

df = pd.read_csv(r'/Users/michaelsierra-hoffman/Downloads/sample.csv')
print(df)

THIS DID NOT WORK This WAS the code used in Section 5.

import pandas as pd

df = pd.read_csv('sample.csv')
print(df)

FileNotFoundError: [Errno 2] File sample.csv does not exist: 'sample.csv'

It’s likely that your code is looking in the wrong location for this file.
There a few ways of dealing with this. I normally use the os module. Something like the following would allow you to run the code without the absolute filepath by changing the current directory but there’s plenty of other methods.

import os
os.chdir(r'/Users/michaelsierra-hoffman/Downloads/')
df = pd.read_csv('sample.csv')

I agree with @tgrtim, this is almost certainly a problem with the file path you gave Pandas. The easiest way to fix this is probably just to move your sample.csv from Downloads into the same folder with your python file. Then you can just use df = pd.read_csv('sample.csv')