pandas 如何在python中使用panda在现有excel表中附加列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43135047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:18:52  来源:igfitidea点击:

how to append columns in existing excel sheet using panda in python

pythonexcelpandasdataframeappend

提问by kumud

import pandas as pd

from pandas import ExcelWriter

trans=pd.read_csv('HMIS-DICR-2011-12-Manipur-Bishnupur.csv')
df=trans[["April 10-11","May 10-11","June 10-11","July 10-11","August 10-11","September 10-11","October 10-11","November 10-11","December 10-11","January 10-11","February 10-11","March 10-11","April 11-12","May 11-12","June 11-12","July 11-12","August 11-12","September 11-12","October 11-12","November 11-12","December 11-12","January 11-12","February 11-12","March 11-12"]]
writer1 = ExcelWriter('manipur1.xlsx')
df.to_excel(writer1,'Sheet1',index=False)
writer1.save()

this code successfully writes the data in a sheet 1 but how can append data of another data frame(df) from different excel file(mention below) into existing sheet(sheet1) "manipur1" excel file

此代码成功地将数据写入工作表 1 中,但如何将来自不同 excel 文件(下文提及)的另一个数据框(df)的数据附加到现有工作表(工作表 1)“manipur1”excel 文件中

for example: my data frame is like:

例如:我的数据框是这样的:

 trans=pd.read_csv('HMIS-DICR-2013-2014-Manipur-Bishnupur.csv')
    df=trans[["April 12-13","May 12-13","June 12-13","July 12-13","August 12-13","September 12-13","October 12-13","November 12-13","December 12-13","January 12-13","February 12-13","March 12-13","April 13-14","May 13-14","June 13-14","July 13-14","August 13-14","September 13-14","October 13-14","November 13-14","December 13-14","January 13-14","February 13-14","March 13-14"]]

采纳答案by pansen

You can only append new data to an existing excel file while loading the existing data into pandas, appending the new data, and saving the concatenated data frame again.

在将现有数据加载到 Pandas、附加新数据并再次保存连接的数据框时,您只能将新数据附加到现有 excel 文件中。

To preserve existing sheets which are supposed to remain unchanged, you need to iterate over the entire workbook and handle each sheet. Sheets to be changed and appended are defined in the to_updatedictionary.

要保留应该保持不变的现有工作表,您需要遍历整个工作簿并处理每个工作表。要更改和附加的工作表在to_update字典中定义。

# get data to be appended
trans=pd.read_csv('HMIS-DICR-2011-12-Manipur-Bishnupur.csv')
df_append = trans[["April 12-13","May 12-13","June 12-13","July 12-13","August 12-13","September 12-13","October 12-13","November 12-13","December 12-13","January 12-13","February 12-13","March 12-13","April 13-14","May 13-14","June 13-14","July 13-14","August 13-14","September 13-14","October 13-14","November 13-14","December 13-14","January 13-14","February 13-14","March 13-14"]]

# define what sheets to update
to_update = {"Sheet1": df_append}

# load existing data
file_name = 'manipur1.xlsx'
excel_reader = pd.ExcelFile(file_name)

# write and update
excel_writer = pd.ExcelWriter(file_name)

for sheet in excel_reader.sheet_names:
    sheet_df = excel_reader.parse(sheet)
    append_df = to_update.get(sheet)

    if append_df is not None:
        sheet_df = pd.concat([sheet_df, append_df], axis=1)

    sheet_df.to_excel(excel_writer, sheet, index=False)

excel_writer.save()

However, any layouting/formatting in your existing excel will be lost. You can use openpyxlif you want to retain the formatting but this is more complicated.

但是,现有 Excel 中的任何布局/格式都将丢失。如果你想保留格式,你可以使用openpyxl但这更复杂。