Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何跳过数据框中的页眉和页脚数据?_Python_Pandas - Fatal编程技术网

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

Python 如何跳过数据框中的页眉和页脚数据?,python,pandas,Python,Pandas,我将excel文件的前15行作为“标题数据”。 235行之后是“页脚数据”。 我需要读取这些页眉和页脚数据之间的数据 有没有办法通过使用pandas选择特定范围的行来将数据读入数据帧?演示: 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

我将excel文件的前15行作为“标题数据”。 235行之后是“页脚数据”。 我需要读取这些页眉和页脚数据之间的数据

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

演示:

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)

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

您可以尝试以下方法:

import pandas as pd

df = pd.read_excel(somefile.xls)

df = df[15:236] #we have to include row 235

总而言之。页眉位置从顶部开始为15,页脚位置从底部开始为Y。以下是如何导入正确的值:

import pandas as pd
df=pd.read_excel("File.xls",header=15,skipfooter=_Y_)

确保没有排除列数据

您也可以在加载文件后执行此操作:

df=df[(df.index>15)&(df.index<236)]
df.index-=16

df=df[(df.index>15)&(df.indexThanks!将尝试此方法,但我需要删除一些列,但显示错误:ExcelFile不支持del columnsdf=df.take(列表(范围(18245)))我不知道SkippFooter现在是如何被弃用的,你应该使用skipfooter intead,它接受结尾的行数作为值skip@MaxU干杯,现在好多了。把新值放在哪里让人困惑。删除Excel标记。这个问题不是关于使用Excel的。@mob你用Excel标记了这个问题。我把它拿走了。