具有多个工作表和特定列的 Pandas read_excel()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41128526/
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
Pandas read_excel() with multiple sheets and specific columns
提问by Binoy Fernandez
I'm trying to use pandas.read_excel()
to import multiple worksheets from a spreadsheet. If I do not specify the columns with the parse_cols keyword I'm able to get all the data from the sheets, but I can't seem to figure out how to specify specific columns for each sheet.
我正在尝试使用pandas.read_excel()
从电子表格导入多个工作表。如果我不使用 parse_cols 关键字指定列,我可以从工作表中获取所有数据,但我似乎无法弄清楚如何为每个工作表指定特定的列。
import pandas as pd
workSheets = ['sheet1', 'sheet2', 'sheet3','sheet4']
cols = ['A,E','A,E','A,C','A,E']
df = pd.read_excel(excelFile, sheetname=workSheets, parse_cols='A:E') #This works fine
df = pd.read_excel(excelFile, sheetname=workSheets, parse_cols=cols) #This returns empty dataFrames
Does anyone know if there is a way, using read_excel(), to import multiple worksheets from excel, but also specify specific columns based on which worksheet?
有谁知道是否有办法使用 read_excel() 从 excel 导入多个工作表,而且还可以根据哪个工作表指定特定列?
Thanks.
谢谢。
采纳答案by ayhan
When you pass a list of sheet names to read_excel
, it returns a dictionary. You can achieve the same thing with a loop:
当您将工作表名称列表传递给 时read_excel
,它会返回一个字典。您可以使用循环实现相同的目的:
workSheets = ['sheet1', 'sheet2', 'sheet3', 'sheet4']
cols = ['A,E', 'A,E', 'A,C', 'A,E']
df = {}
for ws, c in zip(workSheets, cols):
df[ws] = pd.read_excel(excelFile, sheetname=ws, parse_cols=c)
Below is update for Python 3.6.5 & Pandas 0.23.4:
以下是 Python 3.6.5 和 Pandas 0.23.4 的更新:
pd.read_excel(excelFile, sheet_name=ws, usecols=c)