如何跳过熊猫数据框中的页眉和页脚数据?

mob*_*mob 6 python pandas

我有一个Excel文件的前15行作为“标题数据”。并在235行之后输入“页脚数据”。我需要读取这些页眉和页脚数据之间的数据

有没有办法通过使用熊猫选择特定范围的行来将数据读入DataFrame?

Max*_*axU 8

演示:

xl = pd.ExcelFile(filepath)

# parsing first (index: 0) sheet
total_rows = xl.book.sheet_by_index(0).nrows

skiprows = 15
nrows = 235 - 15

# calc number of footer rows
# (-1) - for the header row
skipfooter = total_rows - nrows - skiprows - 1

df = xl.parse(0, skiprows=skiprows, skipfooter=skipfooter)
Run Code Online (Sandbox Code Playgroud)


A S*_*sh 6

您对第15行到第235行的数据感兴趣。

你可以试试这个:

import pandas as pd

df = pd.read_excel(somefile.xls)

df = df[15:236] #we have to include row 235
Run Code Online (Sandbox Code Playgroud)


小智 6

所以总结一下。页眉位置距顶部15,页脚位置距底部Y。以下是导入正确值的方法:

import pandas as pd
df=pd.read_excel("File.xls",header=15,skipfooter=_Y_)
Run Code Online (Sandbox Code Playgroud)

请确保您的柱状数据不会被排除在外!