0

Good day everyone. I have a .csv file that I want to read from my drive. I am using colab to do that. However, I am using excel to setup the csv file, but when I specify the location on my colab it still shows .xlsx with it, and I have this error below:

ParserError                               Traceback (most recent call last)
<ipython-input-4-b8dede7d2e2c> in <module>()
      7 
      8 #load dataset
----> 9 dataset = pd.read_csv('/content/mnt/MyDrive/Colab Notebooks/salary_data.csv.xslx')
     10 
     11 # split data into features and target

3 frames
/usr/local/lib/python3.6/dist-packages/pandas/io/parsers.py in read(self, nrows)
   2155     def read(self, nrows=None):
   2156         try:
-> 2157             data = self._reader.read(nrows)
   2158         except StopIteration:
   2159             if self._first_chunk:

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader.read()

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._read_low_memory()

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._read_rows()

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._tokenize_rows()

pandas/_libs/parsers.pyx in pandas._libs.parsers.raise_parser_error()

ParserError: Error tokenizing data. C error: Expected 1 fields in line 4, saw 2

This is the code below: model.ipynb

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
import pickle
from sklearn.metrics import r2_score

#load dataset
dataset = pd.read_csv('/content/mnt/MyDrive/Colab Notebooks/salary_data.csv .xslx')

# split data into features and target
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, 1].values

#split the data into train and test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.05, random_state = 0)

# create a model
regressor = LinearRegression()

#train the model
regressor.fit(X_train, y_train)

#perform prediction
y_pred = regressor.predict(X_test)

# you can check the peformance of the model from the following code
#print("R2 score: {}".format(r2_score(y_test,y_pred)))

#save the trained model
pickle.dump(regressor, open('/content/mnt/MyDrive/Colab Notebooks/regressor.pkl','wb'))

Help me with this please. Thank you

3
  • if it shows it as .xslx then maybe you still have excel and you should use read_excel() instead if read_csv()
    – furas
    Jan 6, 2021 at 21:46
  • if this is real csv then you can open it in any text editor and check if you have correct data. OR maybe you used separator different then comma and now you would have to use the same separator in read_csv(..., sep=...)
    – furas
    Jan 6, 2021 at 21:48
  • Thanks it is working
    – Kyom Dogo
    Jan 7, 2021 at 12:37

1 Answer 1

1

First of all, it looks like there's an error in your path. There's a space near the end.

('/content/mnt/MyDrive/Colab Notebooks/salary_data.csv .xslx')

Secondly, is the file actually a .csv or a .xslx? It has both endings so it's a bit ambiguous.

If it's a .csv, you should remove the .xslx from the end of filename and the path.

If it's a .xlsx, you can use read_excel() instead of read_csv() or you can convert to CSV in Excel. Open the .xslx in Excel -> go to File -> save as -> CSV.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.