본문 바로가기
코딩 연습

파이썬 판다스(pandas) 활용한 엑셀 데이터 분리

by good4me 2023. 3. 26.

goodthings4me.tistory.com

하나의 엑셀 시트에 데이터가 20만 Row개 데이터(Row x Column: 200000 x 36)가 있고, 이것을 1만 여개씩 분리하여 각각 엑셀 파일로 저장하고 싶을 때 사용하면 유용한 코드

 

 

python pandas와 numpy 활용 엑셀 데이터 분리

import pandas as pd
import numpy as np

def file_seperate():
    seperate_row_cnt = 10000
    file_no = 1
    df =pd.read_excel('./data/엑셀시트데이터.xlsx')
    for chunk in np.array_split(df, len(df) // seperate_row_cnt):
        chunk.to_excel(f'./data/시트_{str(file_no).zfill(2)}.xlsx', index=False)
        print(f'엑셀파일 분리: {str(file_no).zfill(2)}')
        file_no += 1

file_seperate()

data 폴더에 있는 "엑셀시트데이터.xlsx"라는 파일을 "시트01.xlsx", "시트02.xlsx",... 파일명으로 분리

 

good4me.co.kr

 

 

댓글