판다스에서 엑셀로 저장할 때 여러개의 시트에 한번에 저장할 수 있나요?
조회수 3160회
판다스에서 데이터프레임을 엑셀로 저장할 때, 한 번에 여러 시트를 만들 수 있나요? df.to_excel() 은 그냥 시트 하나만 만들어지는데 첫 번째 데이터 프레임은 "a"라는 첫 번째 시트에, 두 번째 데이터 프레임은 "b" 라는 두 번째 시트에 저장되도록 하려면 어떤 방법이 있을까요?
ExcelWriter로 이미 저장된 파일을 불러와서 추가하는 방법밖에 없나요?
-
(•́ ✖ •̀)
알 수 없는 사용자
1 답변
-
pandas to_excel multiple sheets
로 구글링 해 보았습니다.src : https://xlsxwriter.readthedocs.io/example_pandas_multiple.html
# # An example of writing multiple dataframes to worksheets using Pandas and # XlsxWriter. # # Copyright 2013-2020, John McNamara, jmcnamara@cpan.org # import pandas as pd # Create some Pandas dataframes from some data. df1 = pd.DataFrame({'Data': [11, 12, 13, 14]}) df2 = pd.DataFrame({'Data': [21, 22, 23, 24]}) df3 = pd.DataFrame({'Data': [31, 32, 33, 34]}) # Create a Pandas Excel writer using XlsxWriter as the engine. writer = pd.ExcelWriter('pandas_multiple.xlsx', engine='xlsxwriter') # Write each dataframe to a different worksheet. df1.to_excel(writer, sheet_name='Sheet1') df2.to_excel(writer, sheet_name='Sheet2') df3.to_excel(writer, sheet_name='Sheet3') # Close the Pandas Excel writer and output the Excel file. writer.save()
댓글 입력