여러 excel 시트에 걸쳐 pandas dataframe을 작성하는 방법


동일한 통합 문서 내의 여러 Excel 시트에 쓰려는 여러 pandas DataFrame이 있는 경우가 종종 있습니다.

다행히도 pandas ExcelWriter() 함수를 사용하여 이 작업을 수행할 수 있습니다. 이 기능을 사용하려면 먼저 xlsxwriter가 설치되어 있는지 확인해야 합니다.

 pip install xlsxwriter

또한 xlwt가 설치되어 있는지 확인해야 합니다.

 pip install xlwt

일단 설치되면 여러 Excel 시트에 걸쳐 여러 Pandas DataFrame을 쉽게 작성할 수 있습니다.

 import pandas as pd

#create three DataFrames
df1 = pd.DataFrame({'dataset': ['A', 'B', 'C', 'D', 'E']})
df2 = pd.DataFrame({'dataset': [13, 15, 15, 17, 22, 24, 29, 30]})
df3 = pd.DataFrame({'dataset': [3, 6, 6]})

#create a Pandas Excel writer using XlsxWriter as the engine
writer = pd. ExcelWriter (' dataframes.xlsx ', engine=' xlsxwriter ')

#write each DataFrame to a specific sheet
df1. to_excel (writer, sheet_name=' first dataset ')
df2. to_excel (writer, sheet_name=' second dataset ')
df3. to_excel (writer, sheet_name=' third dataset ')

#close the Pandas Excel writer and output the Excel file
writer.save()

결과 Excel 통합 문서에는 각 Pandas DataFrame이 별도의 시트에 저장됩니다.

첫 번째 DataFrame:

Pandas 여러 DataFrame을 여러 Excel 시트로

두 번째 DataFrame:

Pandas는 여러 Excel 시트로 내보내기

세 번째 DataFrame:

pandas 여러 Excel 스프레드시트

추가 리소스

Pandas에서 여러 Excel 시트를 결합하는 방법
Pandas로 Excel 파일을 읽는 방법
Pandas로 CSV 파일을 읽는 방법

의견을 추가하다

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다