programing

판다 사용 두 개의 서로 다른 Excel 파일/시트 결합/합병

subpage 2023. 8. 12. 10:18
반응형

판다 사용 두 개의 서로 다른 Excel 파일/시트 결합/합병

저는 두 개의 다른 엑셀 파일을 결합하려고 합니다.(여러 Excel 파일을 python panda로 가져와서 하나의 데이터 프레임에 연결합니다.)

제가 지금까지 연습한 것은:

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")

이것이 그들의 모습입니다.

enter image description here

하지만 저는 다음을 원합니다.

  1. 각 파일의 마지막 행(예: 파일 1.xls의 경우 4행 및 5행, 파일 2.xls의 경우 7행 및 8행)을 제외합니다.
  2. 데이터의 출처를 나타내는 열을 추가하거나 열 A를 덮어씁니다.

예:

enter image description here

가능합니까?감사해요.

1번의 경우 다음과 같이 지정할 수 있습니다.skip_footer여기에 설명된 바와 같이; 또는, 또는, 을 합니다.

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)
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

바꾸다

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

로.

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

큰따옴표를 가지고 놀아야 할 수도 있지만, 저는 그것이 효과가 있을 것 같습니다.

언급URL : https://stackoverflow.com/questions/25400240/using-pandas-combining-merging-2-different-excel-files-sheets

반응형