我有一个大的电子表格文件(.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)

其他回答

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

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

将只上传表"sheet_name"。

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

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

df = pd.read_excel('FileName.xlsx', 'SheetName')

这将从文件FileName.xlsx中读取表SheetName

你也可以使用表的索引:

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

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

sheet2 = xls.parse(1)

根据用例有不同的选项:

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

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

有关查找表名等信息,请参阅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.