我目前有此代码。它运作完美。
它循环浏览文件夹中的excel文件,删除前两行,然后将它们另存为单个excel文件,还将循环中的文件另存为附加文件。
当前,每次我运行代码时,附加文件都会覆盖现有文件。
我需要将新数据附加到已经存在的Excel工作表('master_data.xlsx)的底部
dfList = []path = 'C:\\Test\\TestRawFile' newpath = 'C:\\Path\\To\\New\\Folder'for fn in os.listdir(path):
# Absolute file path
file = os.path.join(path, fn)
if os.path.isfile(file):
# Import the excel file and call it xlsx_file
xlsx_file = pd.ExcelFile(file)
# View the excel files sheet names
xlsx_file.sheet_names
# Load the xlsx files Data sheet as a dataframe
df = xlsx_file.parse('Sheet1',header= None)
df_NoHeader = df[2:]
data = df_NoHeader
# Save individual dataframe
data.to_excel(os.path.join(newpath, fn))
dfList.append(data) appended_data = pd.concat(dfList)appended_data.to_excel(os.path.join(newpath, 'master_data.xlsx'))我认为这将是一个简单的任务,但我想不是。我想我需要将master_data.xlsx文件作为数据框导入,然后将索引与新添加的数据进行匹配,然后将其保存回来。或者,也许有一种更简单的方法。任何帮助表示赞赏。
哔哔one
长风秋雁
随时随地看视频慕课网APP
相关分类