在标题后通过pandas.read_excel跳过行范围 [英] Skipping range of rows after header through pandas.read_excel

查看:2617
本文介绍了在标题后通过pandas.read_excel跳过行范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道pandas.read_excel()中的参数usecols允许您选择特定的列.

I know the argument usecols in pandas.read_excel() allows you to select specific columns.

说我用pandas.read_excel()读取了一个Excel文件.我的Excel电子表格有1161行.我想保留第一行(索引为0),并跳过2:337行.似乎参数skiprows仅在涉及0索引时才有效.我不知道我是否会错,但是我的代码多次运行总是产生读取 all 我的1161行的输出,而不是仅在第337行之后.像这样:

Say I read an Excel file in with pandas.read_excel(). My excel spreadsheet has 1161 rows. I want to keep the 1st row (with index 0), and skip rows 2:337. Seems like the argument skiprows works only when 0 indexing is involved. I don't know if I could be wrong, but several runs of my code always produces an output of reading all my 1161 rows rather than only after the 337th row on. Such as this:

documentationscore_dataframe = pd.read_excel("Documentation Score Card_17DEC2015 Rev 2 17JAN2017.xlsx",
                                        sheet_name = "Sheet1",
                                        skiprows = "336",
                                        usecols = "H:BD")

这是我所设置内容的另一种尝试.

Here is another attempt of what I have set up.

documentationscore_dataframe = pd.read_excel("Documentation Score Card_17DEC2015 Rev 2 17JAN2017.xlsx",
                                        sheet_name = "Sheet1",
                                        skiprows = "1:336",
                                        usecols = "H:BD")

我希望数据框排除原始Excel导入中的第2到337行.

I would like the dataframe to exclude rows 2 through 337 in the original Excel import.

推荐答案

根据pandas.read_excel,skiprows的="noreferrer">文档必须类似于列表.

As per the documentation for pandas.read_excel, skiprows must be list-like.

请尝试执行此操作以排除第1至336行(包括第1行和第336行)

Try this instead to exclude rows 1 to 336 inclusive:

df = pd.read_excel("file.xlsx",
                   sheet_name = "Sheet1",
                   skiprows = range(1, 337),
                   usecols = "H:BD")

注意:为此,将range构造函数视为list,因此无需显式列表转换.

Note: range constructor is considered list-like for this purpose, so no explicit list conversion is necessary.

这篇关于在标题后通过pandas.read_excel跳过行范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆