我对python和程序设计完全陌生。
我想做著名的“泰坦尼克号”数据科学项目,但是尽管我上传了CSV文件,但是却无法读取。我使用Jupyter和Python 3。
我多次下载了所有CSV文件。
# 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]
错误:FileNotFoundError
我应该更改目录路径吗?但是,如果是这样的话?
答案 0 :(得分:2)
为了安全起见,请在两个文件中均提供csv文件的full
路径以加载数据框
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]
或者,如果您知道csv文件的目录,请使用os.path.join
创建完整路径
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]
此外,如果您知道csv文件与运行脚本的位置相同,则也可以使用os.path.dirname(__file__)
来获取当前文件夹。