简体   繁体   English

如何使用python2.7.14更新具有不同数据框的同一Excel工作表而不会覆盖excel文件中存在的先前数据?

[英]How to update the same excel sheet with different dataframe without overwriting the previous data present in excel file using python2.7.14?

Lets say I have 3 different dataframe which I am generating the dataframe in looping. 可以说我有3个不同的数据框,它们正在循环中生成数据框。 So currently I am converting the data frame to excel file. 所以目前我正在将数据框转换为excel文件。 I am able to convert only one data frame to excel. 我只能将一个数据帧转换为excel。 But I need to update all the 3 dataframes with different data should be appended one after the other in excel file. 但是我需要用不同的数据更新所有3个数据帧,应在excel文件中一个接一个地追加。 How can I achieve that? 我该如何实现? Is it possible to do? 有可能吗?

My Current code. 我当前的代码。 I am converting different dataframe to different sheets of the excel file. 我将不同的数据框转换为Excel文件的不同工作表。

Note: I need to append to only one excel sheet . 注意:我只需要追加到一张Excel工作表

for itr in range(0,3):
        df["users"] = UserArray
        for action in range(len(ColumnName)):
            Array = []
            for user in range(int(usercount)): 
                #calling Copylogs function to copy the required logs and actions in script into another file        
                Array.append(CopyLogs(abs_read_path,itr+1,user+1,ColumnName[action]))
            Array1 = AverageStatistics(Array)
            df[ColumnName[action]] = Array1
        print df
        print "\n"
        df.to_excel(writer,sheet_name="TimeStampsOfItr="+str(itr+1))
    writer.save()

Do you mean writing the dataframes to different sheets in the file? 您的意思是将数据帧写入文件中的不同工作表吗? That can be done with the sheet_name= parameter of pandas.DataFrame.to_excel although you'll have to make sure each write is complete and the file released before doing the next one. 可以使用pandas.DataFrame.to_excel的sheet_name =参数完成此操作,尽管在执行下一个操作之前,必须确保每次写入均已完成并释放了文件。 Try moving the writer.save() into the loop. 尝试将writer.save()移入循环。 If you don't want to do that establish the writer instance separately and use it for multiple dataframes on different sheets. 如果您不想这样做,请分别建立writer实例,并将其用于不同工作表上的多个数据框。

If you want to successively write to the same sheet in the excel file but in different places that overlap in space you'll need to compose the different data together in pandas and then write to the target sheet from a single dataframe. 如果要连续写入excel文件中的同一张纸,但要在空间上重叠的不同位置写入数据,则需要将不同的数据以大熊猫的形式组合在一起,然后从单个数据框中写入目标纸。 The particulars of pandas writing to excel don't provide a mechanism for writing just odd rows then just even rows, for example. 例如,熊猫向excel写入的详细信息没有提供仅写入奇数行然后仅写入偶数行的机制。

Edit: more information added to question 编辑:更多信息添加到问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python:将数据框更新到现有的excel工作表,而不会覆盖同一工作表和其他工作表上的内容 - python: update dataframe to existing excel sheet without overwriting contents on the same sheet and other sheets 如何使用 python 脚本将一行中存在的数据(以空格分隔)拆分为同一 excel 工作表中的不同列? - How to split data present in a row (separated by space) into different column in the same excel sheet using python script? 如何使用aws eb控制台在aws beantalk上为python2.7.14程序创建环境应用程序? - How to create a environment-application on aws beanstalk for a python2.7.14 program using aws eb console? 如何从源代码中在python2.7.14安装中包含pip包 - How to include the pip package in python2.7.14 installation from sources 如何在不覆盖数据的情况下写入现有的 excel 文件(使用熊猫)? - How to write to an existing excel file without overwriting data (using pandas)? Python-如何防止for循环覆盖相同的Excel工作表 - Python - How to prevent for loop overwriting same excel sheet Python:覆盖 Excel 文件中的现有工作表 - Python: overwriting an existing sheet in Excel file 使用python在现有Excel文件中的不同工作表中的相同Excel中的新工作表中合并结果摘要 - Consolidating Result Summary in new sheet in same excel from different sheets on a existing excel file using python 在不覆盖数据的情况下向现有 excel 文件添加新行(Python) - Add new row to existing excel file without overwriting the data (Python) excel表中没有数据 - Data not present in excel sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM