I am trying to write a simple function that exports the resulting dataframe to my local drive.
def finding(dataset, x='target'):
test=dataset.loc[dataset['Title']==x]
return
test.to_excel(r'C:\testing.xlsx')
This is how I executed the function.
finding(df, x='Manager')
However, I am not able see the excel file on my local drive.
Am I missing something?
Thanks.
Related
Anyone know to to upload a .csv file to a folder inside a blob container in python?
i'm having difficulty trying to acess the folders inside it.
i have the csv and want to save it inside the blob folder, but it didn't work.
the file is in code, so i dont want to pass the directory where it is.
csv = df.to_csv()
block_blob_service.create_blob_from_path(container_name, 'folder/csv/mycsv/' , csv)
Someone knows how i can save the csv directly to the folder inside the storage folder (folder/csv/mycsv/) in azure?
i got an error stat: path too long for Windows
Reading the documentation of DataFrame.to_csv, I believe csv variable actually contains string type data. If that's the case, then you will need to use create_blob_from_text method.
So your code would be:
csv = df.to_csv()
block_blob_service.create_blob_from_text(container_name, 'folder/csv/mycsv/' , csv)
I am trying to save the .xlsx file and using VBA as a wrapper to execute the python file. However, I can validate that python code runs, but somehow .xlsx file is not saved. When I run the same python file via IDE, it saves down the .xlsx file.
VBA code
Sub RunPython()
Dim shell As Object
Dim exepath, scriptPath As String
Set Shell = VBA.CreateObject("Wscript.Shell")
exePath = """C:\Program Files\...\python.exe"""
scriptPath = "C:\....\mymodule.py"
Python Script
import pandas as pd
import xlsxwriter
df = pd.DataFrame(np.random.randn(5,2),index=range(0,10,2),columns =list('AB'))
excel_file ='sample.xlsx'
writer = pd.ExcelWriter(excel_file,engine='xlsxwriter')
df.to_excel(writer,sheet_name='Data')
print ("file read")
writer.save()
writer.close()
Does that 'excel_file' have a path?
Does Python use a current directory and automatically add it to Excel file name?
If yes, do you know how to obtain it and can you look for .xlsx file there?
If not, how do you check if the .xlsx file has been saved? Where are you looking for?
Does Python raise any error?
I am looking to the code mostly like a VBA user...
I would suggest to send a message from Python, containing the current directory path. Supposing that the .xlsx workbook path has not somehow been defined and the code presented here does not include that part. Theoretically it should use the same current directory, but I think it is good to be checked...
I have an ever increasing list of file paths (i have around 5000 records now) in Excel. More specifically, I have a certain unique identifier in column A and in Column B, I have a file path that leads to a picture for that unique identifier.
The process of adding the file paths is very manual and sometimes mistakes occur. So, I wanted to create a code that goes through each one of this file paths and if file path doesn't open/returns an error, to store these values in a list so that I can go directly to those and fix the file path.
I was thinking of writing a Python code that checks the File Path in Google Chrome URL (I have found it to work better than directly clicking the Hyperlink in Excel), but it's been a while since I have used Python and don't know where to start.
Any recommendation/ideas of how to achieve this?
Thank you,
Ricardo G.
To read excel files, I prefer to use the pandas library, specifically the read_excel function. You can also check if a filepath is a valid, existing file in your filesystem using the os.path module. os.path.isfile returns True if the provided path points to an actual file, so you want to use a list comprehension with a filter to only have filepaths where that is not the case.
import pandas as pd
import os
df = pd.read_excel('path/to/excel')
bad_files = [fp for fp in df['filepath_column'] if !os.path.isfile(path)]
I'm not sure what you mean by check with google chrome, but if you're talking about local files, this should work well for you.
I am trying to run Excel macros from ABAQUS software tool which has inbuilt Python.
I have a default Excel file, I want this as a main Excel file and to make a new Excel file depending on the output database file.
I access the ABAQUS output data base file and get its name and make a new Excel file named by combining the output data base file name plus some other name.
I copy the main Excel file to this new Excel file.
I am able to open this newly created file, but I am unable to run the macros.
Here is the code with comments.
I get the path of my file with name and extension
filePath = odbFile.path
Filename with extension only
filename_w_ext = os.path.basename(filePath)
odbfilename,odbfileExtension = os.path.splitext(filename_w_ext)
excel_filename='abc.xlsm'
odbfilename+'_FAT'+'.xlsm'
newExcelFile = odbfilename+'_FAT'+'.xlsm'
shutil.copy(excel_filename, newExcelFile)
I access the win32client to access the Excel application
xl = win32com.client.Dispatch("Excel.Application")
Opening the Excel file, give the file path
xl.Workbooks.Open(newExcelFile)
Say newExcelFile created name is new.xlsm.
To run the Excel macros using PYTHON, the default code is
xl.Application.Run("excelName!ModuleNumberOfTheMacro.MacroName")
Run my macro to import nodes from Excel
xl.Application.Run("newExcelFile!Module1.ImporterFichiersTextes")
I put down the same format but Python is looking for the Excel sheet 'newExcelFile', instead of new.xlsm.
I am creating a new dataframe in pandas as below:
df = pd.read_excel(zipfile.open('zipfilename 2017-06-28.xlsx'), header=1, names=cols)
The single .xlsx within the .zip is dynamically named (so changes based on the date).
This means I need to change the name of the .xlsx in my code each time I open the .zip to account for the dynamically named .xlsx.
Is there a way to make pandas read the file within the .zip, regardless of the name of the file? Or to return the name of the .xlsx within the line of code somehow?
Thanks
Read your file using,
df = pd.read_excel('zipfilename 2017-06-28.xlsx',compression='zip', header=1, names=cols)