我要在现有的excel文件下附加一个数据框。在以下代码的帮助下,数据框已成功附加到Excel文件下方,但数据框的索引值显示为我的第一列,即列A中。
例如-
1。我现有的excel文件正在关注-
A B C
10 20 30
30 40 50
我的数据框遵循df1-
a = {'A':[10,11,12],'B':[12,13,14],'C':[14,15,16]} df1 = pd.DataFrame(a)#要附加到现有Excel文件下面的数据框
3。运行代码后,我将遵循Output-
A B C D
10 20 30
30 40 50
0 10 11 12
1 12 13 14
2 14 15 16
4。但是我希望输出如下(期望的输出)
A B C
10 20 30
30 40 50
10 11 12
12 13 14
14 15 16
5。第3步中显示的输出包含索引值0、1和2,但我不希望该索引值,而是希望A列必须附加在A列下方,B列必须附加在B列下方,C列必须附加在C列下方。
简单地说,我不希望索引值出现在我的excel文件中,因为使用索引值会弄错Excel文件。
由于我的数据框中存在索引值,因此我的数据框数据正在向右移动一列。
我的代码是
#Below is a method to write dataframe value below an existing excel file
def append_df_to_excel(filename, df, sheet_name='Sheet1', startrow=None [truncate_sheet][1]=False,
**to_excel_kwargs):
"""
Append a DataFrame [df] to existing Excel file [filename]
into [sheet_name] Sheet.
If [filename] doesn't exist, then this function will create it.
Parameters:
filename : File path or existing ExcelWriter
(Example: '/path/to/file.xlsx')
df : dataframe to save to workbook
sheet_name : Name of sheet which will contain DataFrame.
(default: 'Sheet1')
startrow : upper left cell row to dump data frame.
Per default (startrow=None) calculate the last row
in the existing DF and write to the next row...
truncate_sheet : truncate (remove and recreate) [sheet_name]
before writing DataFrame to Excel file
to_excel_kwargs : arguments which will be passed to `DataFrame.to_excel()`
[can be dictionary]
Returns: None
"""
from openpyxl import load_workbook
import pandas as pd
# ignore [engine] parameter if it was passed
if 'engine' in to_excel_kwargs:
to_excel_kwargs.pop('engine')
writer = pd.ExcelWriter(filename, engine='openpyxl')
# Python 2.x: define [FileNotFoundError] exception if it doesn't exist
try:
FileNotFoundError
except NameError:
FileNotFoundError = IOError
try:
# try to open an existing workbook
writer.book = load_workbook(filename)
# get the last row in the existing Excel sheet
# if it was not specified explicitly
if startrow is None and sheet_name in writer.book.sheetnames:
startrow = writer.book[sheet_name].max_row
# truncate sheet
if truncate_sheet and sheet_name in writer.book.sheetnames:
# index of [sheet_name] sheet
idx = writer.book.sheetnames.index(sheet_name)
# remove [sheet_name]
writer.book.remove(writer.book.worksheets[idx])
# create an empty sheet [sheet_name] using old index
writer.book.create_sheet(sheet_name, idx)
# copy existing sheets
writer.sheets = {ws.title:ws for ws in writer.book.worksheets}
except FileNotFoundError:
# file does not exist yet, we will create it
pass
if startrow is None:
startrow = 1
# write out the new sheet
df.to_excel(writer, sheet_name, startrow=startrow, **to_excel_kwargs)
# save the workbook
writer.save()
import pandas as pd
a = {'A':[10,11],'B':[12,13],'C':[14,15]}
df1 = pd.DataFrame(a) #df1 is datafame which I want to append
path1 = r"C:\Users\Desktop\ab.xlsx" #PATH OF EXISTING EXCEL FILE
append_df_to_excel(path1, df1, sheet_name='Sheet1')
答案 0 :(得分:4)
根据docs,如果您不想编写行名,则将索引参数设置为false。
即
df.to_excel(writer, sheet_name, startrow=startrow, **to_excel_kwargs)
应该是
df.to_excel(writer, sheet_name, startrow=startrow, , index=False, **to_excel_kwargs)
在代码的完整上下文中(带有一些内务处理):
import pandas as pd
from openpyxl import load_workbook
def append_df_to_excel(filename, df, sheet_name='Sheet1', startrow=1, truncate_sheet=False, **to_excel_kwargs):
# ignore [engine] parameter if it was passed
if 'engine' in to_excel_kwargs:
to_excel_kwargs.pop('engine')
writer = pd.ExcelWriter(filename, engine='openpyxl')
try:
# try to open an existing workbook
writer.book = load_workbook(filename)
# get the last row in the existing Excel sheet
# if it was not specified explicitly
if startrow is None and sheet_name in writer.book.sheetnames:
startrow = writer.book[sheet_name].max_row
# truncate sheet
if truncate_sheet and sheet_name in writer.book.sheetnames:
# index of [sheet_name] sheet
idx = writer.book.sheetnames.index(sheet_name)
# remove [sheet_name]
writer.book.remove(writer.book.worksheets[idx])
# create an empty sheet [sheet_name] using old index
writer.book.create_sheet(sheet_name, idx)
# copy existing sheets
writer.sheets = {ws.title: ws for ws in writer.book.worksheets}
except FileNotFoundError:
# file does not exist yet, we will create it
pass
if startrow is None:
startrow = 1
# write out the new sheet
df.to_excel(writer, sheet_name, startrow=startrow, index=False, **to_excel_kwargs)
# save the workbook
writer.save()
if __name__ == '__main__':
a = {'A': [10, 11], 'B': [12, 13], 'C': [14, 15]}
df1 = pd.DataFrame(a) # df1 is datafame which I want to append
path1 = r"C:\Users\Desktop\ab.xlsx" # PATH OF EXISTING EXCEL FILE
append_df_to_excel(path1, df1, sheet_name='Sheet1')
答案 1 :(得分:0)
我找到了我所问问题的解决方案。
在调用以下函数时
append_df_to_excel(path1, df1, sheet_name='Sheet1', header=None,index = False )
我们必须传递header = None和index = False值。 完成后,我们将获得所需的输出。
谢谢