【问题标题】:Using pandas Combining/merging 2 different Excel files/sheets使用 pandas 组合/合并 2 个不同的 Excel 文件/工作表
【发布时间】:2014-10-13 13:07:12
【问题描述】:

我正在尝试合并 2 个不同的 Excel 文件。 (感谢帖子Import multiple excel files into python pandas and concatenate them into one dataframe

到目前为止我的工作是:

import os
import pandas as pd

df = pd.DataFrame()

for f in ['c:\\file1.xls', 'c:\\ file2.xls']:
    data = pd.read_excel(f, 'Sheet1')
    df = df.append(data)

df.to_excel("c:\\all.xls")

这是他们的样子。

但我想:

  1. 排除每个文件的最后一行(即 File1.xls 中的 row4 和 row5;File2.xls 中的 row7 和 row8)。
  2. 添加一列(或覆盖 A 列)以指示数据的来源。

例如:

有可能吗?谢谢。

【问题讨论】:

    标签: python excel pandas


    【解决方案1】:

    对于数字。 1、可以指定skip_footer解释here;或者,或者,做

    data = data.iloc[:-2]
    

    一旦您读取数据。

    对于数字。 2、你可以这样做:

    from os.path import basename
    data.index = [basename(f)] * len(data)
    

    另外,最好将所有数据帧放在一个列表中,然后在最后concat它们;类似:

    df = []
    for f in ['c:\\file1.xls', 'c:\\ file2.xls']:
        data = pd.read_excel(f, 'Sheet1').iloc[:-2]
        data.index = [os.path.basename(f)] * len(data)
        df.append(data)
    
    df = pd.concat(df)
    

    【讨论】:

    • 壮丽,我不得不说。 behzad.nouri,你太棒了!
    【解决方案2】:
    import os
    import os.path
    import xlrd
    import xlsxwriter
    
    file_name = input("Decide the destination file name in DOUBLE QUOTES: ")
    merged_file_name = file_name + ".xlsx"
    dest_book = xlsxwriter.Workbook(merged_file_name)
    dest_sheet_1 = dest_book.add_worksheet()
    dest_row = 1
    temp = 0
    path = input("Enter the path in DOUBLE QUOTES: ")
    for root,dirs,files in os.walk(path):
        files = [ _ for _ in files if _.endswith('.xlsx') ]
        for xlsfile in files:
            print ("File in mentioned folder is: " + xlsfile)
            temp_book = xlrd.open_workbook(os.path.join(root,xlsfile))
            temp_sheet = temp_book.sheet_by_index(0)
            if temp == 0:
                for col_index in range(temp_sheet.ncols):
                    str = temp_sheet.cell_value(0, col_index)
                    dest_sheet_1.write(0, col_index, str)
                temp = temp + 1
            for row_index in range(1, temp_sheet.nrows):
                for col_index in range(temp_sheet.ncols):
                    str = temp_sheet.cell_value(row_index, col_index)
                    dest_sheet_1.write(dest_row, col_index, str)
                dest_row = dest_row + 1
    dest_book.close()
    book = xlrd.open_workbook(merged_file_name)
    sheet = book.sheet_by_index(0)
    print "number of rows in destination file are: ", sheet.nrows
    print "number of columns in destination file are: ", sheet.ncols
    

    【讨论】:

      【解决方案3】:

      改变

      df.to_excel("c:\\all.xls")
      

      df.to_excel("c:\\all.xls", index=False)
      

      您可能需要使用双引号,但我认为这会起作用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-07-04
        • 2020-08-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-13
        • 2016-11-15
        相关资源
        最近更新 更多