Panda's ExcelWriter append mode not working - python

I don't understand why mode='a' or even mode='w' is not working.
My code is :
file = "Data_Complet.xlsx"
df1 = pd.read_excel(file, encoding='utf-8')
df_transfo = DataFrame(df1, columns = ['DATE','Nb'])
print(df_transfo)
with ExcelWriter(path="Data_Complet.xlsx", mode='a') as writer:
df_transfo.to_excel(writer)
The result is:
TypeError: init() got an unexpected keyword argument 'mode'.
I am using Spyder.

I keep coming to this question and forget what I did! So, I'm adding the code for it.
As stated in the comments, you need to have pandas version greater than 0.24.0.
Just run
pip install --upgrade pandas

Related

<ValueError: Sheet already exists and if_sheet_exists is set to 'error'> on one machine but not on another

I'm running some basic python code which generates a pandas DataFrame called df and then writes it to a pre-formatted Excel file using pandas ExcelWriter and openpyxl as its engine.
workbook = load_workbook('example.xlsx')
sheet = workbook['example_sheet']
writer = pd.ExcelWriter('example.xlsx', engine='openpyxl', mode='a')
writer.book = workbook
writer.sheets = {ws.title: ws for ws in writer.book.worksheets}
sheet.cell(row=8, column=5).value = some.value
df.to_excel(writer, sheet_name='example_sheet', index=False, header=False, startrow=10, startcol=5)
The strange thing is that when running on my machine it works perfectly, however on my colleague's machine it throws the mentioned error:
File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\excel\_openpyxl.py", line 437, in write_cells f"Sheet '{sheet_name}' already exists and " ValueError: Sheet 'example_sheet' already exists and if_sheet_exists is set to 'error'.
I've tried working around it by explicitly setting
if_sheet_exists = 'replace'
however now (as expected) it replaces the entire sheet and destroys the formatting that was applied before. When running on my machine however it would not replace the sheet, even though the writer was set to do so.
I'm not entirely sure where to look for differences in the machines so if anyone could throw me an idea I would be very thankful.
The last Pandas version to behave as you expect was version 1.2.5. pip uninstall pandas and then pip install pandas==1.2.5, then it will simply append the data to your previously formatted Excel template.
YMMV.
Edit:
I believe the issue was with openpyxl, which you may be able to revert separately if the above solution is not feasible. You might also try if_sheet_exists = 'overlay' if it suits your needs.

Python appending dataframe to exsiting excel file and sheet

I have a question about appending a dataframe to existing sheet on existing file.
I tried to write the code by myself
writer = pd.ExcelWriter('existingFile.xlsx', engine='openpyxl', mode='a')
df.to_excel(writer, sheet_name="existingSheet", startrow=writer.sheets["existingSheet"].max_row, index=False, header=False)
and this would cause an error
ValueError: Sheet 'existingSheet' already exists and if_sheet_exists is set to 'error'.
and I googled and found this function in here;
Append existing excel sheet with new dataframe using python pandas
and even with this function, it still causes the same error, even though i think this function prevents this exact error from what i think.
Could you please help?
Thank you very much!
I guess I am late to the party but
Add keyword if_sheet_exists='replace' to ExcelWriter like this
pd.ExcelWriter('existingFile.xlsx', engine='openpyxl', mode='a', if_sheet_exists='replace' )
Then you can use the latest version of pandas
Since Pandas 1.4, there is also an 'overlay' option to if_sheet_exists.
It seems this function is broken in pandas 1.3.0 Just look at this document , when trying to append to existing sheet it will either raise an error, create a new one or replace it
if_sheet_exists{‘error’, ‘new’, ‘replace’}, default ‘error’ How to
behave when trying to write to a sheet that already exists (append
mode only).
error: raise a ValueError.
new: Create a new sheet, with a name determined by the engine.
replace: Delete the contents of the sheet before writing to it.
New in version 1.3.0
The only solution is to downgrade pandas (1.2.3 is working for me)
pip install pandas==1.2.3

TypeError: to_excel() missing 1 required positional argument - despite using excel writer

I've been having trouble saving to excel using pandas with the following error:
File "C:/Users/Colleen/Documents/Non-online code/kit_names.py", line 36, in save_sheet_names
pd.DataFrame.to_excel(writer)
TypeError: to_excel() missing 1 required positional argument: 'excel_writer'
here
After having typed this:
df =pd.DataFrame(arr)
y=os.path.basename(os.path.normpath(path))
new_path = r"C:\Users\Colleen\Documents\\"+y
writer = pd.ExcelWriter(new_path, engine='xlsxwriter')
pd.DataFrame.to_excel(writer)
Seemingly the writer is being used but evidently i have gone wrong somewhere! (I apologise if this is an obvious question as i am still getting my head around pandas)
Pycharm is signalling a minor error which may be what is causing this however i don't understand what it means in truth and struggled to find a mention online.
passing pandas.io.excel.excelwriter instead of pandas.core.frame.dataframe. Is this intentional?
What do i need to do to fix this please?
Simply change the to_excel() function to be caled on df
df =pd.DataFrame(arr)
y=os.path.basename(os.path.normpath(path))
new_path = r"C:\Users\Colleen\Documents\\"+y
writer = pd.ExcelWriter(new_path, engine='xlsxwriter')
df.to_excel(writer)

Openpyxl not removing sheets from created workbook correctly

So I ran into an issue with remove_sheet() with openpxyl that I can't find an answer to. When I run the following code:
import openpyxl
wb = openpyxl.Workbook()
ws = wb.create_sheet("Sheet2")
wb.get_sheet_names()
['Sheet','Sheet2']
wb.remove_sheet('Sheet')
I get the following error:
ValueError: list.remove(x): x not in list
It doesn't work, even if I try wb.remove_sheet(0) or wb.remove_sheet(1), I get the same error. Is there something I am missing?
If you use get_sheet_by_name you will get the following:
DeprecationWarning: Call to deprecated function get_sheet_by_name (Use
wb[sheetname]).
So the solution would be:
xlsx = Workbook()
xlsx.create_sheet('other name')
xlsx.remove(xlsx['Sheet'])
xlsx.save('some.xlsx')
remove.sheet() is given a sheet object, not the name of the sheet!
So for your code you could try
wb.remove(wb.get_sheet_by_name(sheet))
In the same vein, remove_sheet is also not given an index, because it operates on the actual sheet object.
Here's a good source of examples (though it isn't the same problem you're facing, it just happens to show how to properly call the remove_sheet method)!
Since the question was posted and answered, the Openpyxl library changed.
You should not use wb.remove(wb.get_sheet_by_name(sheet)) as indicated by #cosinepenguin since it is now depreciated ( you will get warnings when trying to use it ) but wb.remove(wb[sheet])
In python 3.7
import openpyxl
wb = openpyxl.Workbook()
ws = wb.create_sheet("Sheet2")
n=wb.sheetnames
#sheetname =>get_sheet_names()
wb.remove(wb["Sheet"])
'#or can use'
wb.remove(wb[n[1]])
1 is index sheet "sheet"
you can visit this link for more info

Why won't pandas.read_excel run?

I am trying to use pandas.read_excel but I keep getting " 'module' object has no attribute 'read_excel' " as an error in my terminal as shown
File "read.py", line 9, in <module>
cols = pd.read_excel('laucnty12', 'Poverty Data', index_col='State', \\ na_values=['NA'])
AttributeError: 'module' object has no attribute 'read_excel'
I have tried pd.read_excel() and pd.io.parsers.read_excel() but get the same error. I have python 2.7 installed and other parts of pandas work fine such as xls.parse and read_csv. My code is below:
import pandas as pd
from pandas import *
xls = pd.ExcelFile('laucnty12.xls')
data = xls.parse('laucnty12', index_col=None, na_values=['NA'])
cols = pd.read_excel('laucnty12', 'Poverty Data', index_col='State', na_values=['NA'])
print cols
df = pd.read_excel(filepath + 'Result.xlsx')
Check whether the extension of excel file is xls or xlsx then add the same in the query. I tried and its is working fine now.
You probably mean pd.io.excel.read_excel()
The problem is that your script is called "read.py". The Python file that defines read_excel already imports another module called "read" - so when you try and run your "read.py" script, it squashes the old "read" module that pandas is using, and thus breaks read_excel. This problem can happen with other "common" short names for scripts, like "email.py".
Try renaming your script.

Categories