Search code examples
pythonnameerror

NameError: in the file directory I'm trying to load a data for analysis


I started to use Google Colab on Chrome on MacOSX and encountered the issue below.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
#matplotlib inline

df = pd.read_csv(/Users/Shin/Box/PROJECTS/SOKENDAI/python_lesson/able_programing/titanic/'train.csv')
df.head

Then I get the error below. I am a beginner and not sure if the way I put file directory was wrong. I've tried "c:Users""~Users", that did not work. Thank you for your help.

NameError Traceback (most recent call last) in () ----> 1 df = pd.read_csv(~Users/Shin/Box/PROJECTS/SOKENDAI/python_lesson/able_programing/titanic/'train.csv') 2 df.head

NameError: name 'Users' is not defined


Solution

  • /Users/Shin/Box/PROJECTS/SOKENDAI/python_lesson/able_programing/titanic/train.csv needs to be a string. Python has no way of telling if that's a file path or not.

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    #matplotlib inline
    
    df = pd.read_csv('/Users/Shin/Box/PROJECTS/SOKENDAI/python_lesson/able_programing/titanic/train.csv')
    df.head