I am trying to take some new data that I have created from some old data and I want to save the new data in a different directory separate from the original directory from where I got the original data. I believe I have the correct data path but I don't think I am using the correct method being called to both create the csv and put it in the newly created directory. I have the code what I was suggested:
#create the appropriate data path
datapath = '../data'
#save the dataframe as a csv file in a new directory
save_file(ski_data, 'ski_data_cleaned.csv', datapath)
I get an error:
NameError: name 'save_file' is not defined
I was understanding the 'save_file' was the method and I'm not sure how to include the 'datapath' in other methods?
try below one:
Call to_csv method on your dataframe. you need to pass the CSV file path as an argument for the method.
ski_data.to_csv("../data/ski_data_cleaned.csv")
If you need to save without headers then use the following one.
ski_data.to_csv("../data/ski_data_cleaned.csv", header=False, index=False)
To save a specific location
#For windows
ski_data.to_csv(r"C:\Users\Admin\Desktop\data\ski_data_cleaned.csv")
Check out the official site for more details.
I want to create a file .zip in python that includes an entire folder + another file,how can i make it?
files.zip:
|-mydir
|-file.txt
Thanks
Solved.
In according with zip documentation
with ZipFile('Your_zip_file.zip','w') as zip:
# writing each file one by one
for file in os.listdir(str(Path("folder_to_zip/"))):
zip.write('folder_to_zip/'+str(file))
zip.close()
Use:
'r' to read an existing file,
'w' to truncate and write a new file,
'a' to append to an existing file, or
'x' to exclusively create and write a new file.
Hope this will be helpful for others.
I have a file with extension .mod.
That file contains fields and data under each field, just like every csv files do.
I need to read this .mod file using Python.
Please suggest me a way out in Python using Pandas or any other package that could help me with this.
Thanks!
On Windows 10, using Python 3.6, I could successfully open the file and read the first line:
with open('09nested.mod') as f:
print(f.readlines()[0])
// File: 09nested.mod
>>>
I'm trying to store a pandas dataframe to a tempfile in csv format (in windows), but am being hit by:
[Errno 13] Permission denied: 'C:\Users\Username\AppData\Local\Temp\tmpweymbkye'
import tempfile
import pandas
with tempfile.NamedTemporaryFile() as temp:
df.to_csv(temp.name)
Where df is the dataframe. I've also tried changing the temp directory to one I am sure I have write permissions:
tempfile.tempdir='D:/Username/Temp/'
This gives me the same error message
Edit:
The tempfile appears to be locked for editing as when I change the loop to:
with tempfile.NamedTemporaryFile() as temp:
df.to_csv(temp.name + '.csv')
I can write the file in the temp directory, but then it is not automatically deleted at the end of the loop, as it is no longer a temp file.
However, if I change the code to:
with tempfile.NamedTemporaryFile(suffix='.csv') as temp:
training_data.to_csv(temp.name)
I get the same error message as before. The file is not open anywhere else.
I encountered the same error message and the issue was resolved after adding "/df.csv" to file_path.
df.to_csv('C:/Users/../df.csv', index = False)
Check your permissions and, according to this post, you can run your program as an administrator by right click and run as administrator.
We can use the to_csv command to do export a DataFrame in CSV format. Note that the code below will by default save the data into the current working directory. We can save it to a different folder by adding the foldername and a slash to the file
verticalStack.to_csv('foldername/out.csv').
Check out your working directory to make sure the CSV wrote out properly, and that you can open it! If you want, try to bring it back into python to make sure it imports properly.
newOutput = pd.read_csv('out.csv', keep_default_na=False, na_values=[""])
ref
Unlike TemporaryFile(), the user of mkstemp() is responsible for deleting the temporary file when done with it.
With the use of this function may introduce a security hole in your program. By the time you get around to doing anything with the file name it returns, someone else may have beaten you to the punch. mktemp() usage can be replaced easily with NamedTemporaryFile(), passing it the delete=False paramete.
Read more.
After export to CSV you can close your file with temp.close().
with tempfile.NamedTemporaryFile(delete=False) as temp:
df.to_csv(temp.name + '.csv')
temp.close()
Sometimes,you need check the file path that if you have right permission to read and write file. Especially when you use relative path.
xxx.to_csv('%s/file.csv'%(file_path), index = False)
Sometimes, it gives that error simply because there is another file with the same name and it has no permission to delete the earlier file and replace it with the new file.
So either name the file differently while saving it,
or
If you are working on Jupyter Notebook or a other similar environment, delete the file after executing the cell that reads it into memory. So that when you execute the cell which writes it to the machine, there is no other file that exists with that name.
I encountered the same error. I simply had not yet saved my entire python file. Once I saved my python file in VS code as "insertyourfilenamehere".py to documents(which is in my path), I ran my code again and I was able to save my data frame as a csv file.
As per my knowledge, this error pops up when one attempt to save the file that have been saved already and currently open in the background.
You may try closing those files first and then rerun the code.
Just give a valid path and a file name
e.g:
final_df.to_csv('D:\Study\Data Science\data sets\MNIST\sample.csv')
I have multiple directories that require the same large json file. Rather than storing the json file multiple times, I would like to store it in one directory and create an alias that points to the actual file in the other directories.
How do I create a shortcut/alias for a file in Python?
You can create a symlink to the file. In python this looks like:
import os
os.symlink("my_json_file.json", "the_link");