我正在尝试将2个excel文件中的数据写入一个文件。当然,下面的代码仅将第一张工作表中的数据写入合并后的工作表中,因为从第一张工作表中写入数据时循环结束。我认为解决方案是保留第一张工作表中的数据,然后追加然后写入到工作表中。但是如何?今天,我太笨了,找不到解决方案。
import pandas as pd
xlsInPath = "some path to sheets but all have one sheet with name 2019"
xlsFiles = ['test1.xlsx', 'test2.xlsx']
sheetName = ['2019']
df = pd.DataFrame({})
for xlsF in xlsFiles:
FN = xlsInPath + xlsF
print(FN)
data1 = pd.read_excel(FN, sheet_name=sheetName, header=1, skiprows=0, engine='xlrd')
print(data1)
df.append(data1, ignore_index=True)
df = pd.concat(data1)
df.to_excel (r'C:\\Users\\A\\out\\Name.xlsx', sheet_name='Sheet_name_1', index=False, header=None)
答案 0 :(得分:0)
我认为您的问题是您正在编写数据框以在循环中表现出色。将循环中的最后一行移到循环外,然后将整个数据框一次粘贴到电子表格中,而不是一次粘贴一张。您的代码中可能有也可能没有其他问题,但是我至少会尝试一下。
答案 1 :(得分:0)
解决方案是:
import pandas as pd
xlsInPath = "C:\\Users\\in\\"
xlsOutPath = "C:\\Users\\out\\"
xlsInFiles = ['test1.xlsx', 'test2.xlsx']
xlsOutFile = 'ConcatResult.xlsx'
sheetName = '2019'
# must be string if only 1 worksheet otherwise a list of strings
df = pd.DataFrame()
for xlsF in xlsInFiles:
FN = xlsInPath + xlsF
print(FN)
xd = pd.read_excel(FN, sheet_name=sheetName, header=0, skiprows=0, engine='xlrd')
# needs header here line 0 first line to recognize the equal columns
print('##XD\n', xd)
df = pd.concat([df, xd], axis=0, ignore_index=True, sort=False)
# needs a list of concatanable DataFrames
print('##DF\n', df)
df.to_excel(r''+xlsOutPath+xlsOutFile, sheet_name=sheetName, index=False, header=1)