从第 5 行开始读取 excel 到 python 数据框并包括标题 [英] reading excel to a python data frame starting from row 5 and including headers
问题描述
如何将excel数据导入python中的数据框.
how do I import excel data into a dataframe in python.
基本上,当前的 excel 工作簿在打开时会运行一些 vba,它会刷新数据透视表并执行一些其他操作.
Basically the current excel workbook runs some vba on opening which refreshes a pivot table and does some other stuff.
然后我希望将数据透视表刷新的结果导入python中的数据框以进行进一步分析.
Then I wish to import the results of the pivot table refresh into a dataframe in python for further analysis.
import xlrd
wb = xlrd.open_workbook('C:UserscbMachine_LearningcMap_Joins.xlsm')
#sheetnames
print wb.sheet_names()
#number of sheets
print wb.nsheets
文件的刷新和打开工作正常.但是我如何从第 5 行的第一张工作表中选择数据,包括标题到最后一条记录 n.
The refreshing and opening of the file works fine. But how do i select the data from the first sheet from say row 5 including header down to last record n.
推荐答案
你可以使用 pandas 的 ExcelFile parse
读取 Excel 表格的方法,参见 io 文档:
You can use pandas' ExcelFile parse
method to read Excel sheets, see io docs:
xls = pd.ExcelFile('C:UserscbMachine_LearningcMap_Joins.xlsm')
df = xls.parse('Sheet1', skiprows=4, index_col=None, na_values=['NA'])
skiprows
将忽略前 4 行(即从行索引 4 开始)和几个 其他选项.
skiprows
will ignore the first 4 rows (i.e. start at row index 4), and several other options.
这篇关于从第 5 行开始读取 excel 到 python 数据框并包括标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!