xxxxxxxxxx
# Save single dataframe to a single sheet
df.to_excel(excel_writer= 'file_name.xls',sheet_name='sheet_1',
startrow=1, startcol=1) # These are optional args
# Alternative approach : use context manager
# Save multiple dataframes in different sheets
df_list = [df1,df2,df3]
sheet_list = ['sheetA','sheetB','sheetC']
for df, sheet in zip(df_list,sheet_list):
with pd.ExcelWriter('file_name.xlsx') as writer:
df.to_excel(excel_writer=writer, sheet_name=sheet)
xxxxxxxxxx
df.to_excel(r'C:\Users\Ron\Desktop\File_Name.xlsx', index = False)
#if you omit the file path (must also omit 'r') and
#enter only file_name.xlsx, it will save to your default file location,
# that is, where the file you're reading from is located.
xxxxxxxxxx
# Python, pandas
# To export a pandas dataframe into Excel
df.to_excel(r'Path where you want to store the exported excel file\File Name.xlsx', index = False)
xxxxxxxxxx
import pandas as pd
# Assuming you have a DataFrame named 'df'
df = pd.DataFrame({'Column1': [1, 2, 3], 'Column2': ['A', 'B', 'C']})
# Saving DataFrame as an Excel file
df.to_excel('output.xlsx', index=False)
xxxxxxxxxx
# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('LTD Report Data.xlsx', engine='xlsxwriter')
# Write each dataframe to a different worksheet.
seg_2019.to_excel(writer, sheet_name='Seg 2019', index = False)
seg_2020.to_excel(writer, sheet_name='Seg 2020', index = False)
seg_2021.to_excel(writer, sheet_name='Seg 2021', index = False)
seg_2022.to_excel(writer, sheet_name='Seg 2022', index = False)
# Close the Pandas Excel writer and output the Excel file.
writer.save()
xxxxxxxxxx
df.to_excel('pandas_to_excel.xlsx', sheet_name='new_sheet_name')
xxxxxxxxxx
import pandas as pd
# Assuming you have a dataframe called 'data'
data = pd.DataFrame({'Column1': [1, 2, 3],
'Column2': ['A', 'B', 'C']})
# Specify the path where you want to save the Excel file
excel_file_path = 'path/to/save/file.xlsx'
# Export the dataframe to Excel
data.to_excel(excel_file_path, index=False)
xxxxxxxxxx
import numpy as np
import pandas as pd
import openpyxl
data_df = pd.DataFrame(mean)
data_df.columns = ['A','B','C','D','E','F','G','H','I','J'] #将第一行的0,1,2,...,9变成A,B,C...
data_df.index = ['a','b','c','d','e','f','g','h','i','j']
writer = pd.ExcelWriter('test.xlsx') # 创建名称为test的excel表格
data_df.to_excel(writer, 'page_1',
float_format='%.2f') # float_format 精度,将data_df写到test表格的第一页中。若多个文件,可以在page_2中写入
writer.save() # 保存
xxxxxxxxxx
In [6]: titanic.to_excel("titanic.xlsx", sheet_name="passengers", index=False)
xxxxxxxxxx
import pandas as pd
# Assuming you have a DataFrame called 'df'
# Specify the path and file name for the Excel file
excel_file_path = 'path/to/excel_file.xlsx'
# Convert the DataFrame to an Excel file
df.to_excel(excel_file_path, index=False)
# Optionally, if you want to include the index in the Excel file:
# df.to_excel(excel_file_path)