I cannot import CSV file?

Asked by At

I am completely new to python and programming in general.

I want to do the Famous "Titanic" Data Science Project but I am unable to read the CSV file although I uploaded them. I use Jupyter and Python 3.

I downloaded all CSV Files several times.


# data analysis and wrangling
import pandas                as pd
import numpy                 as np
import random                as rnd

# visualization
import seaborn               as sns
import matplotlib.pyplot     as plt
%matplotlib inline

# machine learning
from sklearn.linear_model  import LogisticRegression
from sklearn.svm           import SVC, LinearSVC
from sklearn.ensemble      import RandomForestClassifier
from sklearn.neighbors     import KNeighborsClassifier
from sklearn.naive_bayes   import GaussianNB
from sklearn.linear_model  import Perceptron
from sklearn.linear_model  import SGDClassifier
from sklearn.tree          import DecisionTreeClassifier

##Acquire data

train_df   = pd.read_csv('../input/train.csv')    #here I get the error
test_df    = pd.read_csv('../input/test.csv')
combine    = [train_df, test_df]

Error: FileNotFoundError

Should I change the Directory Path? But if so in what way ?

1 Answers

Devesh Kumar Singh On

Just to be safe, provide the full path of the csv file in both files to load your dataframes

train_df   = pd.read_csv('<path_to_csv>/train.csv')  
test_df    = pd.read_csv('<path_to_csv>/test.csv')
combine    = [train_df, test_df]

Or if you know the directory of the csv file, use os.path.join to create the full path

import os

folder = "<path_to_csv>"

#Full path of csv files
train_path = os.path.join(folder, 'train.csv')
test_path = os.path.join(folder, 'test.csv')

#Use full path to open csv file
train_df = pd.read_csv(train_path)  
test_df = pd.read_csv(test_path)
combine = [train_df, test_df]

Also as an addition, if you know that the csv files are in the same location from where the script is run, you can use os.path.dirname(__file__) to get the current folder as well.