pandas 从不同数据框中的excel读取多个选项卡

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42887663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:13:41  来源:igfitidea点击:

reading multiple tabs from excel in different dataframes

pythonpandas

提问by user2560244

I am trying to read multiple tabs in spreadsheet to different dataframes and once all tabs with data are over the program should stop.

我正在尝试将电子表格中的多个选项卡读取到不同的数据框,一旦所有带有数据的选项卡都结束程序应该停止。

For first part I am looking to do something like

对于第一部分,我希望做类似的事情

xls = pd.ExcelFile('Unique.xlsx') 
for i in range(1,n): # n should be number of tabs with data
 try:
    df_Sector(i)=xls.parse('Sheet'+i) # df_Sector(i) has to be dataframe
 except:
    pass

I want the program to stop once all tabs with data are read

我希望程序在读取所有带有数据的选项卡后停止

回答by MaxU

Demo:

演示:

file name

文档名称

In [94]: fn = r'D:\temp\.data\test.xlsx'

creating pandas.io.excel.ExcelFileobject

创建pandas.io.excel.ExcelFile对象

In [95]: xl = pd.ExcelFile(fn)

it has sheet_namesattribute

它有sheet_names属性

In [96]: xl.sheet_names
Out[96]: ['Sheet1', 'aaa']

we can use it for looping through sheets

我们可以用它来循环工作表

In [98]: for sh in xl.sheet_names:
    ...:     df = xl.parse(sh)
    ...:     print('Processing: [{}] ...'.format(sh))
    ...:     print(df.head())
    ...:
Processing: [Sheet1] ...
   col1  col2  col3
0    11    12    13
1    21    22    23
2    31    32    33
Processing: [aaa] ...
   a  b  c
0  1  2  3
1  4  5  6
2  7  8  9

a bit more elegant way is to generate a dictionary of DataFrames:

一个更优雅的方法是生成一个 DataFrames 字典:

In [100]: dfs = {sh:xl.parse(sh) for sh in xl.sheet_names}

In [101]: dfs.keys()
Out[101]: dict_keys(['Sheet1', 'aaa'])

In [102]: dfs['Sheet1']
Out[102]:
   col1  col2  col3
0    11    12    13
1    21    22    23
2    31    32    33

In [103]: dfs['aaa']
Out[103]:
   a  b  c
0  1  2  3
1  4  5  6
2  7  8  9

回答by b2002

This will read all sheets and make a dictionary of dataframes:

这将读取所有工作表并制作数据框字典:

xl = pd.read_excel('Unique.xlsx', sheetname=None)

To get specific sheets, you could do:

要获取特定的工作表,您可以执行以下操作:

xl_dict = {}
sheetname_list = ['blah1', 'blah2', 'blah3']
for sheet in sheetname_list:
    xl_dict[sheet] = pd.read_excel('Unique.xlsx', sheetname=sheet)

or:

或者:

xl = pd.read_excel('Unique.xlsx', sheetname=sheetname_list)