相关疑难解决方法(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万
查看次数

Python如何使用ExcelWriter写入现有工作表

我正在尝试使用ExcelWriter将一些信息写入/添加到包含多个工作表的工作簿中.我第一次使用该函数时,我正在创建包含一些数据的工作簿.在第二个调用中,我想在不同位置的工作簿中将一些信息添加到所有工作表中.

def Out_Excel(file_name,C,col): 
    writer = pd.ExcelWriter(file_name,engine='xlsxwriter')
    for tab in tabs:    # tabs here is provided from a different function that I did not write here to keep it simple and clean
        df = DataFrame(C)    # the data is different for different sheets but I keep it simple in this case
        df.to_excel(writer,sheet_name = tab, startcol = 0 + col, startrow = 0)
    writer.save()
Run Code Online (Sandbox Code Playgroud)

在主代码中,我使用不同的col调用此函数两次,以在不同位置打印出我的数据.

Out_Excel('test.xlsx',C,0)
Out_Excel('test.xlsx',D,10)
Run Code Online (Sandbox Code Playgroud)

但问题是这样做输出只是函数的第二次调用,就像函数覆盖整个工作簿一样.我想我需要加载本案例中已经存在的工作簿?有帮助吗?

python pandas xlsxwriter

5
推荐指数
2
解决办法
3万
查看次数

标签 统计

pandas ×2

python ×2

append ×1

excel ×1

for-loop ×1

xlsxwriter ×1