相关疑难解决方法(0)

使用python pandas使用新数据框附加现有excel表

我目前有这个代码.它完美地运作.

它循环遍历文件夹中的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 = …
Run Code Online (Sandbox Code Playgroud)

python excel for-loop append pandas

17
推荐指数
4
解决办法
4万
查看次数

标签 统计

append ×1

excel ×1

for-loop ×1

pandas ×1

python ×1