I got a datetime index dataframe with several columns, and I want to plot a graph for each week of a specific column, but I want those graphs to be saved in a specific folder which would be created.
The folder name would be the column name, and the graph name would be 'columnname_week'.
Right now I can save my figs with the good name by using this function :
def graph(a):
for i in range (2,53):
plt.figure(figsize=[20,20])
a19=df19[[a]][(df19["week"]==i)]
plt.plot(a19)
name=str(a)+"_"+str(i)
plt.savefig(name)
return
graph('column_name')
But I can't find a way to save them in a specific folder by the name of the column.
Also I'm using spyder and it seems that all my plots are being saved in C:\Users\Me instead of the working file where I saved my program, and I can't figure out how to change it.
If I am understanding your question correctly, the reason why it saves to "C:\Users\Me" is because your name variable is a relative path then python will save it to the current work directory (cwd).
You can refer to os.makedirs to create directories and os.path to create a absolute path.
For example,
desired_folder = 'D:/some/desired/folder'
os.makedirs(desired_folder)
name=str(a)+"_"+str(i)
full_path = os.path.join(desired_folder, name)
plt.savefig(name)
Related
I have a folder named with a certain acronym, and inside this folder you can find a certain number of Excel files.
The folder's name indicates the name of the apartment (for ex. UDC06_45) and, inside this folder, all of the Excel files' name are composed by:
the name of the apartment, followed by the name of the appliance that is located in that apartment (for ex. UDC06_45_Oven).
These Excel files are very simple DataFrames, they contain energy consumption measurements: one column named "timestamps" and one column named "Energy" (all of these measurements have a 15 min frequency). All of the Excel files inside the folder are made with the same identical structure.
My Python code takes as input only one of these Excel files at a time and makes few operations on them (resampling, time interpolation, etc.) starting with the command "pd.read_excel()", and creates an output Excel file with "df.to_excel()" after giving it a name.
What I want to do is to apply my code automatically to all of the files in that folder.
The code should take as input only the name of the folder ("UDC06_45") and create as many output files as needed.
So if the folder contains only two appliances:
"UDC06_45_Oven"
"UDC06_45_Fridge"
the code will elaborate them both, one after the other, and I should obtain two dinstinct Excel files as output. Their name is just composed by the input file's name followed by "_output":
"UDC06_45_Oven_output"
"UDC06_45_Fridge_output".
In general, this must be done for every Excel file contained in that folder. If the folder contains 5 appliances, meaning 5 input Excel files, I should obtain 5 output Excel files... and so on.
How can I do it?
In the following code only assing your path, in my case I have used a test folder path path=r'D:\test' this code will create a new folder automatically in the same path.
import pandas as pd
import os
from glob import glob
path=r'D:\test' # add whatever your path is in place of 'D:\test'
input_folder='UDC06_45' # name of input folder
output_folder=input_folder+'_out'
new_path=path+'/'+output_folder
if not os.path.exists(new_path):
os.makedirs(new_path)
files=glob(path+'/'+input_folder+'/'+'*.xlsx')
for file in files:
name=file.split(path+'/'+input_folder+'\\')[-1].rsplit('.')[0]
df=pd.read_excel(file)
#do all your operations here
df.to_excel(new_path+'/'+name+'_output.xlsx')
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 have a list with directories.
shapelist
that has:
['C:\\Users\\user\\Desktop\\etg\\v1\\ASTENOT\\ASTENOT.shp',
'C:\\Users\\user\\Desktop\\etg\\v2\\ASTENOT\\ASTENOT.shp',
'C:\\Users\\user\\Desktop\\etg\\v3\\ASTENOT\\ASTENOT.shp',
'C:\\Users\\user\\Desktop\\etg\\v4\\ASTENOT\\ASTENOT.shp']
I want in each loop to use each ASTENOT from the list above which resides in a separate folder.
I have solved this part.
The issue is how to export each outcome in the corresponding folder where each input (each ASTENOT in every loop used) is located.
Example:
I am using this specific function in the loop.
arcpy.FeatureToLine_management(['ASTENOT'],'ASTENOT_lines')
The ['ASTENOT] position is for the input and
the 'ASTENOT_lines' is for the output of the function.
How can I make the output exported in the folder of each corresponding input?
Example: the ASTENOT_lines of the first loop to be exported in the v1\\ASTENOT\\ location the second in v2\\ASTENOT\\ and so on.
My attempt:
for i in shapelist:
arcpy.FeatureToLine_management([i],'ASTENOT_lines')
but exports everything in the current working directory and not in their corresponding folders of their inputs in each loop.
You can pass an absolute path to the FeatureToLine_management method.
The absolute path can be generated by simply replacing ASTENOT.shp in the input path with ASTENOT_lines.
So you can change your code to
for i in shapelist:
outputfile = i.replace('ASTENOT.shp', 'ASTENOT_lines')
arcpy.FeatureToLine_management([i], outputfile)
I am an experienced Maya user but fairly new to programming and there is something I am trying to figure out.
I am attempting to create a function for use inside of Maya that would allow me to search the file structure for a texture file.
What I want it to do is this:
if texture_name is missing:
access folder containing this scene
go up one file to character folder
if textures folder exists:
if object matching texture_name exists:
set this folder as new texture path
alternatively it could do something like this:
texture path is A:B/C/D/texture_name
if A:B/C/D/texture_name == False:
if A:B/C/E/texture_name == False:
if A:B/F/E/texture_name == False:
etc.
replacing the texture path one layer at a time until it works
EDIT:
I have found a workaround that generates the texture paths I need, but I am still looking for a way to Query whether a certain file exists. Like the objExists function but to check for files outside the maya scene.
You can use the os module to check if it exists.
There is os.path, os.path.isfile()
extracting only texture filenames being used in maya file from the list of filename paths
I just began with Python and am having a little difficulty with storing the result of a function in a variable.
I have a small script that does the following:
change to a directory and within that directory:
create a new directory named to the moment it has been created (for example 2016200420161636)
what i want it to do additionally:
create a file within that newly created directory
I would think to be able to have the file created in the newly created directory I need to store the directory name ( 2016200420161636) and return the value to a part of the script that creates the file (so it knows where to write the file to).
Can someone please advise?
Are you looking to just save the value before you create the directory?
something like
timestamp = time.clock()
if not os.path.exists(timestamp)
os.makedirs(timestamp)
now timestamp has the value stored and you can use as needed