【发布时间】:2019-11-28 15:36:16
【问题描述】:
我在现有的 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) #Dataframe要追加到现有的Excel文件下面
3.运行代码后,我得到以下输出-
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')
【问题讨论】:
-
但是我想问什么已经写清楚了?
-
我想在现有的 excel 文件下面附加数据框。但不希望索引值出现在最终的 Excel 工作表中
-
我已经提供了一个可能的答案,但是您的问题的格式仍然很差。尝试逐步重写它,而不是为初学者编写一个巨大的单个段落。就您的代码而言,我不确定在哪里使用
a,因此将其重命名为有意义的名称,然后告诉我们您如何使用此数据a,以及您在代码中的确切位置创建它,修改它等,以及您将其写入 excel 表的位置。我们不想仅仅为了找出问题所在而阅读您的整个代码。您应该尝试为我们缩小范围。 -
您应该查阅
to_excel()方法的文档。 -
你能告诉我我可以在我的代码中做哪些更改以实现所需的输出吗?