I have come across a lot of answers and just wanted to check if this is the best answer
Write pandas dataframe values to excel to specific cell in a specific sheet.
The question is - assuming I have a dataframe "df".
I want to write to an existing excel file called "Name1.xlsx", in
worksheet called "exampleNames", and starting at cell d25.
What's the easiest/ most efficient way to do that.
###############Updated!#############
I tried this
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import openpyxl
path = "C:\\Users\\ABC\\PycharmProjects\\ABC\\Name1.xlsx"
df = pd.DataFrame(np.random.randint(1,10,(3,2)),columns=['a','b'])
df.to_excel(path,sheet_name="exampleNames",startcol=5,startrow=5,header=None,index=False)
df.to_excel(path,sheet_name="NN",startcol=5,startrow=25,header=None,index=False)
gave me error
ModuleNotFoundError: No module named 'openpyxl'
This is the approach suggested in the pandas docs
df.to_excel(writer, sheet_name='Sheet1', startcol=col,startrow=row, header=None)
where writer could be path-like, file-like, or ExcelWriter object
eg
df.to_excel('sample.xlsx',sheet_name="exampleNames",startcol=5,startrow=5,header=None)
To save multiple dataframes in excel, you will have to use the writer object
with pd.ExcelWriter('output.xlsx', engine="openpyxl", mode='a', if_sheet_exists='overlay') as writer:
df1.to_excel(writer, sheet_name='exampleNames' ,startcol=5,startrow=5,header=None,index=False)
df2.to_excel(writer, sheet_name='NN', startcol=5,startrow=25,header=None,index=False)
Related
I' am new to Python and trying to write into a merged cell within Excel. I can see the data that is already stored within this cell/row, so I know its there. However when I try to overwrite it nothing happens.
I have tried messing with the index and header as well but nothing seems to work.
import pandas as pd
from openpyxl import load_workbook
Read the excel file into a pandas DataFrame
df = pd.read_excel(file here', sheet_name='Sheet1')
print(df.iloc[8, 2])
Make the changes to the DataFrame
df.iloc[8, 2] = "Bob Smith"
Load the workbook
book = load_workbook(file here)
writer = pd.ExcelWriter(file here, engine='openpyxl')
writer.book = book
Write the DataFrame to the first sheet
df.to_excel(writer, index=False)
Save the changes to the Excel file
writer.save()
import pandas as pd
from openpyxl import *
file="C:/Users/OneDrive/Bureau/draftExcel.xlsx"
df = pd.read_excel(file,sheet_name='sheet1')
df.iat[5,0]='cell is updated'
print(df) # to check first in the terminal if the content of the cell is updated
book=load_workbook(file)
writer=pd.ExcelWriter(file, engine='openpyxl')
df.to_excel(writer,sheet_name='sheet1',index=False)
writer.close()
I tried to make an example from what you explained because you didn't show your code, so I hope it was helpful.
Instead of using .iloc I used .iat so you can update the data in a specific cell in your DataFrame using column_index instead of column_label.
Remember that the Excel file you are working on must be closed while you are editing data with python, if it is open you will get an error.
I have multiple lists in my python code and i want to copy those lists to different columns in an already existing excel file.
writer = pd.ExcelWriter('sample.xlsx')
pd.DataFrame(timedata).to_excel(writer, 'timedata')
writer.save()
this writes the list to the excel but it always over writes the data in excel and to write multiple lists in multiple columns is not been defined in this code.
Pandas uses openpyxl for xlsx files(mentioned in pd docs). By checking docs for ExcelWriter, you can see that something like this might work out:
import pandas
from openpyxl import load_workbook
book = load_workbook('sample.xlsx')
writer = pandas.ExcelWriter('sample.xlsx', engine='openpyxl')
writer.book = book
## ExcelWriter for some reason uses writer.sheets to access the sheet.
## If you leave it empty it will not know that sheet Main is already there
## and will create a new sheet.
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
#data_filtered is a pd dataframe
data_filtered.to_excel(writer, "Main", cols=['col1', 'col2'])
writer.save()
If you are using pandas version later than 0.24, then the process is even more simplified:
import pandas as pd
with pd.ExcelWriter('sample.xlsx', engine='openpyxl', mode='a') as writer:
data_filtered.to_excel(writer)
I have refined an existing xlsx file and want to create three new files based on the content. Successful in getting three new outputs, but not able to write it to new xlsx files.
I tried installing excelwriter but that didn't fixed my problem.
import pandas as pd
import xlsxwriter
xl_file = pd.ExcelFile('C:\\Users\\python_codes\\myfile.xlsx')
dfs = pd.read_excel('myfile.xlsx', sheetname="Sheet1")
test = dfs.drop_duplicates(subset='DetectionId', keep='first', inplace=False)
dfs2 = test[test['list_set_id'] == 1]
print(dfs2)
writer = dfs2.ExcelWriter('newfile.xlxs', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')
writer.save()
I want to write new xlsx file with the filtered content from the existing file.
ExcelWriter belongs to the pandas module, not to a DataFrame instance.
writer = dfs2.ExcelWriter should be writer = pd.ExcelWriter
Im trying to create an excel file with pandas for a database I have generated.
I have tried both:
import pandas as pd
# write database to excel
df = pd.DataFrame(database)
# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('fifa19.xlsx', engine='xlsxwriter')
# Convert the dataframe to an XlsxWriter Excel object.
df.to_excel(writer, sheet_name='Sheet1')
# Close the Pandas Excel writer and output the Excel file.
writer.save()
as well as:
import pandas as pd
df = pd.DataFrame(database).T
df.to_excel('database.xls')
However, none of the options generate an excel file. Database is a dictionary.
From the pandas document Notes itself:
If passing an existing ExcelWriter object, then the sheet will be added to the existing workbook. This can be used to save different DataFrames to one workbook:
>>> writer = pd.ExcelWriter('output.xlsx')
# writer = pd.ExcelWriter('/path_to_save/output.xlsx')
>>> df1.to_excel(writer,'Sheet1')
>>> df2.to_excel(writer,'Sheet2')
>>> writer.save()
For compatibility with to_csv, to_excel serializes lists and dicts to
strings before writing.
How can I export a list of DataFrames into one Excel spreadsheet?
The docs for to_excel state:
Notes
If passing an existing ExcelWriter object, then the sheet will be added
to the existing workbook. This can be used to save different
DataFrames to one workbook
writer = ExcelWriter('output.xlsx')
df1.to_excel(writer, 'sheet1')
df2.to_excel(writer, 'sheet2')
writer.save()
Following this, I thought I could write a function which saves a list of DataFrames to one spreadsheet as follows:
from openpyxl.writer.excel import ExcelWriter
def save_xls(list_dfs, xls_path):
writer = ExcelWriter(xls_path)
for n, df in enumerate(list_dfs):
df.to_excel(writer,'sheet%s' % n)
writer.save()
However (with a list of two small DataFrames, each of which can save to_excel individually), an exception is raised (Edit: traceback removed):
AttributeError: 'str' object has no attribute 'worksheets'
Presumably I am not calling ExcelWriter correctly, how should I be in order to do this?
You should be using pandas own ExcelWriter class:
from pandas import ExcelWriter
# from pandas.io.parsers import ExcelWriter
Then the save_xls function works as expected:
def save_xls(list_dfs, xls_path):
with ExcelWriter(xls_path) as writer:
for n, df in enumerate(list_dfs):
df.to_excel(writer,'sheet%s' % n)
In case anyone needs an example using a dictionary of dataframes:
from pandas import ExcelWriter
def save_xls(dict_df, path):
"""
Save a dictionary of dataframes to an excel file,
with each dataframe as a separate page
"""
writer = ExcelWriter(path)
for key in dict_df.keys():
dict_df[key].to_excel(writer, sheet_name=key)
writer.save()
example:
save_xls(dict_df = my_dict, path = '~/my_path.xls')
Sometimes there can be issues(Writing an excel file containing unicode), if there are some non supporting character type in the data frame. To overcome it we can use 'xlsxwriter' package as in below case:
for below code:
from pandas import ExcelWriter
import xlsxwriter
writer = ExcelWriter('notes.xlsx')
for key in dict_df:
data[key].to_excel(writer, key,index=False)
writer.save()
I got the error as "IllegalCharacterError"
The code that worked:
%pip install xlsxwriter
from pandas import ExcelWriter
import xlsxwriter
writer = ExcelWriter('notes.xlsx')
for key in dict_df:
data[key].to_excel(writer, key,index=False,engine='xlsxwriter')
writer.save()