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.
Related
I am having some trouble writing an csv to an excel workbook. I have tried a series of options but i think openpyxl migth do job... however i cannot find a way to do this.
e.g.
from openpyxl import Workbook
import openpyxl
import csv
#ceate workbook
wb = openpyxl.Workbook()
TOP3 = wb.sheetnames
wb.create_sheet(title='metrics')
wb.save(filename='Test.xlsx')
sheet = wb["TOP3"]
#write an existing csv to the sheet above:
with open('metrics.csv', 'r') as f:
reader = csv.reader(f)
for row in reader:
for i in range(0,len(row)):
sheet[f"""A{i}"""] = row
above does not work! How can I read a csv file into an excel workbook? I would like to automate this. I basically want to import my metrics csv into the metrics tab of my workbook
I suggest using pandas
import pandas as pd
df = pd.read_csv('metrics.csv')
df.to_excel('test.xlsx', sheet_name='metrics', index=False)
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
I am trying to import the data and create a new excel file first and then I want to add this excel sheet to an existing Excel file.
I did the first step as successfully as follows and I am stuck in doing the second step of adding the new sheet to another existing excel file.
I wish to keep the same existing formatting including the existing formulas in my existing excel sheet. Any help, please? I tried a lot with existing help but all refer to using Pandas for both the files. But in my case, the existing excel file has nothing to do with Pandas data structure since it has many formulas and texts formatted.
from openpyxl import load_workbook
import json
from pprint import pprint
import pandas as pd
data = json.load(open('data.txt'))
# Create a Pandas dataframe from the data.
df = pd.DataFrame({'Data': data["_owner"]["_accountId"]["averageIRR"]},index=["averageIRR"])
# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('data_sheet.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()
The data file is as below:
{
"_id": "58edf90",
"_owner": {
"_id": "57e4611f3a",
"_accountId": {
"_id": "57e294611f3b",
"companyName": "authguys",
"averageIRR": 0
}
}
}
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()