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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45722949/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:14:15  来源:igfitidea点击:

How to skip header and footer data in pandas dataframe?

pythonpandas

提问by mob

I have first 15 rowsof a excel file as "Header data". and after 235 rows, "Footer data". I need to read data in between these header and footer data.

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

Is there any way to read data into DataFrame by selecting specific range of rows using pandas?

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

回答by MaxU

Demo:

演示:

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)

回答by A Santosh

You are interested in data from row 15to row 235.

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

You can try this:

你可以试试这个:

import pandas as pd

df = pd.read_excel(somefile.xls)

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

回答by Kaustubh J

So to summarize. Header location is 15from the top and Footer location is Yfrom the bottom. Here's how you import the correct values:

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

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

Do ensure that your columnar data isn't being excluded!

请确保您的柱状数据没有被排除在外!

回答by Harsha

You can also do this after loading the file:

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

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