带有多个工作表和特定列的 pandas read_excel() [英] Pandas read_excel() with multiple sheets and specific columns
问题描述
我正在尝试使用pandas.read_excel()
从电子表格中导入多个工作表.如果我不使用parse_cols关键字指定列,则可以从工作表中获取所有数据,但似乎无法弄清楚如何为每个工作表指定特定的列.
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.
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
有人知道是否可以使用read_excel()从excel导入多个工作表,还可以根据哪个工作表指定特定的列?
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
时,它将返回字典.您可以通过循环来实现相同的目的:
When you pass a list of sheet names to read_excel
, it returns a dictionary. You can achieve the same thing with a loop:
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)
以下是针对Python 3.6.5&熊猫0.23.4:
Below is update for Python 3.6.5 & Pandas 0.23.4:
pd.read_excel(excelFile, sheet_name=ws, usecols=c)
这篇关于带有多个工作表和特定列的 pandas read_excel()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!