I am coding a python program to read a data set file writing this line:
df = pd.read_csv (r'C:\Users\user118\Desktop\StudentsPerformance.csv')
This line works, but I have to upload this project as an assignment , so the computer path must be changed. I think about putting the csv file in the project folder and i did and wrote this line:
df = pd.read_csv ("StudentsPerformance.csv")
but it gave me an error saying that the file isn't found. Where to correctly put the file in the project folder? Or what I should do?
To read your csv file with a df = pd.read_csv ("StudentsPerformance.csv") line you should put it right next to executing .py file.
To do that you can just read your csv file using full path like this:
df = pd.read_csv (r'C:\Users\user118\Desktop\StudentsPerformance.csv')
and than write df.to_csv('StudentsPerformance.csv')
After that you would be able to read your csv as you wanted using
df = pd.read_csv('StudentsPerformance.csv')
Related
I am trying to read a csv file in python, but I keep getting a "FileNotFoundError". No such file or directory
I have it written as:
file = open('case_study2.csv')
I used:
import os
os.getcwd()
to get the current directory for my python file which came back as:
runfile('/Users/natestewart/casestudy2/Task4_CaseStudy2', wdir='/Users/natestewart/casestudy2')
To read csv files, one way is by using pandas library.
import pandas as pd
path = '/Users/natestewart/casestudy2/Task4_CaseStudy2'
file = pd.read_csv('path/case_study2.csv')
I'm trying to import multiple csv files in one folder into one data frame. This is my code. It can iterate through the files and print them successfully and it can read one file into a data frame but combining them is printing an error. I saw many questions similar but the responses are complex, I thought the 'pythonic' way is to be simple because I am new to this. Thanks in advance for any help. The error message is always: No such file or directory: 'some file name' which makes no sense because it successfully printed the file name in the print step.
import pandas as pd
# this works
df = pd.read_csv("headlines/2017-1.csv")
print(df)
path = 'C:/.../... /.../headlines/' <--- full path I shortened it here
files = os.listdir(path)
print(files) <-- prints all file names successfully
for filename in files:
print(filename) # <-- successfully prints all file names
df = pd.read_csv(filename) # < -- error here
df2.append(df) # append to data frame
It seems like your current working directory is different from your path. Please use
os.chdir(path) before attempting to read your csv.
I have an issue. Here is my code:
import pandas as pd
from pandas import ExcelFile
test = pd.read_excel("C:\\Users\\John\\Desktop\\Python_work\\stock\\zen\\OutputFiles\\Test_file.csv", header=0)
print(test)
My problem is the code does not see the file "Test_file.csv" at all...
I have also tried putting the file in the same directory as the code itself. it still does not see the file. I have used a .txt file just to see if the code can see any file and the code recognizes the .txt file has no columns which is expected. It is as if the code is blind to excel files...
Does anyone have a solution?
You could try using raw string instead (r"")
amzn = pd.read_excel(r"C:\Users\John\Desktop\Python_work\stock\zen\OutputFiles\Test_file.csv", header=0)
I have been trying to read some excel files using pandas but when I use the for loop to go through all the files I get an error
I have checked if the files name are stored in week and they are, actually if I do try to read individually I can read the files, but as soon as I use the for I get this error
import pandas as pd
import os
week = [file for file in os.listdir("./data_excel")]
week_concatenated = pd.DataFrame()
for file in week:
df = pd.read_excel('data_excel/'+file, 'DIGITAL_TASKS')
week_concatenated = pd.concat([week_concatenated, df])
Well, there was actually a file inside the folder that the system created and I didnt see it before, thats why when the loop was started was reading that file and throwing that error as this file was not xlsx.
I'm trying to output an .xlsx file with pd.ExcelWriter as shown below:
writer_t = pd.ExcelWriter('C:/Users/bbecker021/AppData/Local/Programs/Python/Python38/Project_Catalog_Template_IDs.xlsx', engine='xlsxwriter')`
I then append to the file with:
data_requirements_DF.to_excel(writer_t, sheet_name=repo_list[element])
And print out the dataframe being added:
print("---", repo_list[element], "data_requirements---\n", data_requirements_DF)
But when I check the folder path I specify above, there is no file...any help as to check what might be happening?