通过pandas.read_excel跳过标题后的行范围

flo*_*e-y 12 python excel dataframe pandas

我知道参数usecolspandas.read_excel(),您可以选择特定的列。

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")
Run Code Online (Sandbox Code Playgroud)

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")
Run Code Online (Sandbox Code Playgroud)

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

jpp*_*jpp 20

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

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")
Run Code Online (Sandbox Code Playgroud)

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

  • +1 表示它是类似列表的,解决了我读取标题但在它之后跳过第一行的问题(`pd.read_excel(path,skiprows=[1])`) (3认同)