我有一个大的电子表格文件(.xlsx),我正在使用python熊猫处理。碰巧,我需要数据从两个选项卡(表)在那个大文件。其中一个选项卡包含大量数据,而另一个选项卡只有几个方形单元格。

当我在任何工作表上使用pd.read_excel()时,它看起来就像加载了整个文件(而不仅仅是我感兴趣的工作表)。因此,当我使用该方法两次(每个工作表一次)时,我实际上不得不忍受整个工作簿被读取两次(即使我们只使用指定的工作表)。

我如何只加载特定的表与pd.read_excel()?


当前回答

你可以用下面几行来阅读所有的表格

import pandas as pd
file_instance = pd.ExcelFile('your_file.xlsx')

main_df = pd.concat([pd.read_excel('your_file.xlsx', sheet_name=name) for name in file_instance.sheet_names] , axis=0)

其他回答

If:

您需要多个工作表,但不是全部 你需要一个df作为输出

然后,您可以传递一个工作表名称列表。你可以手动填充:

import pandas as pd
    
path = "C:\\Path\\To\\Your\\Data\\"
file = "data.xlsx"
sheet_lst_wanted = ["01_SomeName","05_SomeName","12_SomeName"] # tab names from Excel

### import and compile data ###
    
# read all sheets from list into an ordered dictionary    
dict_temp = pd.read_excel(path+file, sheet_name= sheet_lst_wanted)

# concatenate the ordered dict items into a dataframe
df = pd.concat(dict_temp, axis=0, ignore_index=True)

OR

如果你想要的工作表有一个通用的命名约定,也允许你区分不需要的工作表,那么一点自动化是可能的:

# substitute following block for the sheet_lst_wanted line in above block

import xlrd

# string common to only worksheets you want
str_like = "SomeName" 
    
### create list of sheet names in Excel file ###
xls = xlrd.open_workbook(path+file, on_demand=True)
sheet_lst = xls.sheet_names()
    
### create list of sheets meeting criteria  ###
sheet_lst_wanted = []
    
for s in sheet_lst:
    # note: following conditional statement based on my sheets ending with the string defined in sheet_like
    if s[-len(str_like):] == str_like:
        sheet_lst_wanted.append(s)
    else:
        pass

如果你有兴趣阅读所有的表格并将它们合并在一起。最好最快的方法

sheet_to_df_map = pd.read_excel('path_to_file.xls', sheet_name=None)
mdf = pd.concat(sheet_to_df_map, axis=0, ignore_index=True)

这将把所有的表转换成一个单独的数据帧m_df

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。

你也可以指定表名作为参数:

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

将只上传表"sheet_name"。

根据用例有不同的选项:

如果你不知道床单的名字。 如果表名不相关。 如果有人知道床单的名字。

下面我们将仔细研究每个选项。

有关查找表名等信息,请参阅Notes部分。


选项1

如果你不知道床单的名字

# Read all sheets in your File
df = pd.read_excel('FILENAME.xlsx', sheet_name=None)
    
# Prints all the sheets name in an ordered dictionary
print(df.keys())

然后,根据想要读取的表,可以将每个表传递到特定的数据帧,例如

sheet1_df = pd.read_excel('FILENAME.xlsx', sheet_name=SHEET1NAME)
sheet2_df = pd.read_excel('FILENAME.xlsx', sheet_name=SHEET2NAME)

选项2

如果名字无关紧要,人们只关心纸张的位置。假设一个人只想要第一页

# Read all sheets in your File
df = pd.read_excel('FILENAME.xlsx', sheet_name=None)

sheet1 = list(df.keys())[0]

然后,根据表名,可以将每个表传递给特定的数据框架,例如

sheet1_df = pd.read_excel('FILENAME.xlsx', sheet_name=SHEET1NAME)

选项3

这里我们将考虑已知床单名称的情况。 对于示例,可以考虑有三个表,分别为Sheet1、Sheet2和Sheet3。每一个的内容都是一样的,看起来像这样

     0         1     2
0   85   January  2000
1   95  February  2001
2  105     March  2002
3  115     April  2003
4  125       May  2004
5  135      June  2005

根据个人的目标,有多种方法:

Store everything in same dataframe. One approach would be to concat the sheets as follows sheets = ['Sheet1', 'Sheet2', 'Sheet3'] df = pd.concat([pd.read_excel('FILENAME.xlsx', sheet_name = sheet) for sheet in sheets], ignore_index = True) [Out]: 0 1 2 0 85 January 2000 1 95 February 2001 2 105 March 2002 3 115 April 2003 4 125 May 2004 5 135 June 2005 6 85 January 2000 7 95 February 2001 8 105 March 2002 9 115 April 2003 10 125 May 2004 11 135 June 2005 12 85 January 2000 13 95 February 2001 14 105 March 2002 15 115 April 2003 16 125 May 2004 17 135 June 2005 Basically, this how pandas.concat works (Source): Store each sheet in a different dataframe (let's say, df1, df2, ...) sheets = ['Sheet1', 'Sheet2', 'Sheet3'] for i, sheet in enumerate(sheets): globals()['df' + str(i + 1)] = pd.read_excel('FILENAME.xlsx', sheet_name = sheet) [Out]: # df1 0 1 2 0 85 January 2000 1 95 February 2001 2 105 March 2002 3 115 April 2003 4 125 May 2004 5 135 June 2005 # df2 0 1 2 0 85 January 2000 1 95 February 2001 2 105 March 2002 3 115 April 2003 4 125 May 2004 5 135 June 2005 # df3 0 1 2 0 85 January 2000 1 95 February 2001 2 105 March 2002 3 115 April 2003 4 125 May 2004 5 135 June 2005


注:

If one wants to know the sheets names, one can use the ExcelFile class as follows sheets = pd.ExcelFile('FILENAME.xlsx').sheet_names [Out]: ['Sheet1', 'Sheet2', 'Sheet3'] In this case one is assuming that the file FILENAME.xlsx is on the same directory as the script one is running. If the file is in a folder of the current directory called Data, one way would be to use r'./Data/FILENAME.xlsx' create a variable, such as path as follows path = r'./Data/Test.xlsx' df = pd.read_excel(r'./Data/FILENAME.xlsx', sheet_name=None) This might be a relevant read.