Import txt files with pandas

Asked

Viewed 1,460 times

0

I’m starting in the area of machine Learning, following a website that suggested the following initial model:

import pandas as pd
from sklearn import linear_model
import matplotlib.pyplot as plt

dataframe = pd.read_fwf('brain_body.txt')
x_values = dataframe[['Brain']]
y_values = dataframe[['Body']]

body_reg = linear_model.LinearRegression()
body_reg.fit(x_values,y_values)

plt.scatter(x_values,y_values)
plt.plot(x_values,body_reg.predict(x_values))
plt.show()

But I’m having the following result:

FileNotFoundError: [Errno 2] No such file or directory: 'brain_body.txt'

the txt file is in the same folder as the . py main file.

  • An easier solution is to use the full file path. The error may occur because the python executable program was not added to the PATH (another solution is to add).

  • I tested it here and it worked. How is the file . txt? Try to use pd.read_csv instead of pd.read_fwf or the complete file path.

  • The file is in rows and columns, as if it were a table

  • I’m using through the anaconda, maybe it’s an incompatibility

1 answer

0

Try the way with ".\":

import pandas as pd
from sklearn import linear_model
import matplotlib.pyplot as plt

dataframe = pd.read_fwf('.\\brain_body.txt')
x_values = dataframe[['Brain']]
y_values = dataframe[['Body']]

body_reg = linear_model.LinearRegression()
body_reg.fit(x_values,y_values)

plt.scatter(x_values,y_values)
plt.plot(x_values,body_reg.predict(x_values))
plt.show()
  • It didn’t work, bro, the error continued, but thanks for the reply

  • The file is in the same directory as the python source and project execution?

  • Yes, it is. Both in the same folder

Browser other questions tagged

You are not signed in. Login or sign up in order to post.