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

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

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


当前回答

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

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

将只上传表"sheet_name"。

其他回答

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

有以下几种选择:

将所有表格直接读入有序字典。

import pandas as pd

# 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)

将第一页直接读入数据框架

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

阅读excel文件并获得表格列表。然后选择和装载床单。

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, sheet_name="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)
    # you can also use sheet_index [0,1,2..] instead of sheet name.

感谢@ihightower指出了阅读所有表格的方法,感谢@toto_tico,@red-headphone指出了版本问题。

sheetname: string, int, string /int的混合列表,或None,默认为0 0.21.0版后已移除:使用sheet_name代替Source Link

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

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.

你也可以使用表的索引:

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

将给出第一个工作表。第二张工作表:

sheet2 = xls.parse(1)