program tip

동일한 통합 문서의 여러 워크 시트에 대해 판다를 사용하여 pd.read_excel ()

radiobox 2020. 7. 28. 08:21
반응형

동일한 통합 문서의 여러 워크 시트에 대해 판다를 사용하여 pd.read_excel ()


파이썬 팬더를 사용하여 처리하는 큰 스프레드 시트 파일 (.xlsx)이 있습니다. 큰 파일에서 두 개의 탭의 데이터가 필요합니다. 탭 중 하나에는 많은 양의 데이터가 있고 다른 하나는 몇 정사각형 셀입니다.

모든 워크 시트에서 pd.read_excel ()사용 하면 전체 파일이로드 된 것처럼 보입니다 (관심이있는 워크 시트뿐만 아니라). 따라서이 방법을 두 번 사용하면 (각 시트마다 한 번씩) 전체 통합 문서를 두 번 읽습니다 (특정 시트 만 사용하더라도).

내가 잘못 사용하고 있거나 이런 식으로 제한되어 있습니까?

감사합니다!


시도 pd.ExcelFile:

xls = pd.ExcelFile('path_to_file.xls')
df1 = pd.read_excel(xls, 'Sheet1')
df2 = pd.read_excel(xls, 'Sheet2')

@HaPsantran이 지적했듯이 ExcelFile()통화 중에 전체 Excel 파일을 읽습니다 (이 방법은 보이지 않습니다). 이렇게하면 새 시트에 액세스 할 때마다 동일한 파일을 읽을 필요가 없습니다.

점을 유의 sheet_name인수가 pd.read_excel()(상기와 같이) 시트의 이름이 될 수있는 시트 수를 나타내는 정수 (예를 들면 0, 1, 등), 시트 이름이나 인덱스 또는 목록 None. 목록이 제공되면 키가 시트 이름 / 표시이고 값이 데이터 프레임 인 사전을 반환합니다. 기본값은 단순히 첫 번째 시트 (예 :)를 반환하는 것 sheet_name=0입니다.

경우 None지정, 모든 시트는 같은 반환됩니다 {sheet_name:dataframe}사전.


3 가지 옵션이 있습니다 :

  1. 첫 번째 시트를 데이터 프레임으로 직접 읽습니다.
  2. Excel 파일을 읽고 시트 목록을 얻으십시오. 그런 다음 시트를 선택하여 넣습니다.
  3. 모든 시트를 읽고 사전에 저장하십시오.

코드 샘플 :

import pandas as pd

df = pd.read_excel('excel_file_path.xls')
# this will read the first sheet into df

xls = pd.ExcelFile('excel_file_path.xls')

# Now you can list all sheets in the file
xls.sheet_names
# ['house', 'house_extra', ...]

# to read just one sheet to dataframe:
df = pd.read_excel(file_name, sheetname="house")

# to read all sheets to a map
sheet_to_df_map = {}
for sheet_name in xls.sheet_names:
    sheet_to_df_map[sheet_name] = xls.parse(sheet_name)

최신 정보:

# @ihightower pointed out in the comments that all sheets can be 
# directly read into an ordered dictionary in 1 step

# for pandas version >= 0.21.0
sheet_to_df_map = pd.read_excel(file_name, sheet_name=None)

# for pandas version < 0.21.0
sheet_to_df_map = pd.read_excel(file_name, sheetname=None)

업데이트 2 : @toto_tico 고맙습니다.

시트 이름 : 문자열, INT, 문자열 / int 치의 또는 없음의 혼합 목록, 기본 0 비추천 API 버전 0.21.0 가입일 : 사용 SHEET_NAME 대신 원본 링크


시트에 색인을 사용할 수도 있습니다.

xls = pd.ExcelFile('path_to_file.xls')
sheet1 = xls.parse(0)

첫 번째 워크 시트를 제공합니다. 두 번째 워크 시트의 경우 :

sheet2 = xls.parse(1)

시트 이름을 매개 변수로 지정할 수도 있습니다.

data_file = pd.read_excel('path_to_file.xls', sheetname="sheet_name")

"sheet_name"시트 만 업로드합니다.


pd.read_excel('filename.xlsx') 

기본적으로 통합 문서의 첫 번째 시트를 읽습니다.

pd.read_excel('filename.xlsx', sheet_name = 'sheetname') 

통합 문서의 특정 시트를 읽고

pd.read_excel('filename.xlsx', sheet_name = None) 

Excel에서 Pandas 데이터 프레임까지의 모든 워크 시트를 OrderedDict 유형으로 중첩 된 데이터 프레임을 의미하며 모든 워크 시트는 데이터 프레임 내부에서 수집 된 데이터 프레임으로, 형식은 OrderedDict입니다.


Yes unfortunately it will always load the full file. If you're doing this repeatedly probably best to extract the sheets to separate CSVs and then load separately. You can automate that process with d6tstack which also adds additional features like checking if all the columns are equal across all sheets or multiple Excel files.

import d6tstack
c = d6tstack.convert_xls.XLStoCSVMultiSheet('multisheet.xlsx')
c.convert_all() # ['multisheet-Sheet1.csv','multisheet-Sheet2.csv']

See d6tstack Excel examples

참고URL : https://stackoverflow.com/questions/26521266/using-pandas-to-pd-read-excel-for-multiple-worksheets-of-the-same-workbook

반응형