Skipping range of rows after header through pandas.read_excel
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")
Note: range
constructor is considered list
-like for this purpose, so no explicit list conversion is necessary.
Related videos on Youtube
florence-y
Updated on March 21, 2021Comments
-
florence-y about 3 years
I know the argument
usecols
inpandas.read_excel()
allows you to select specific columns.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 argumentskiprows
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")
I would like the dataframe to exclude rows 2 through 337 in the original Excel import.
-
mrbTT about 5 years+1 for expliciting it's list-like, resolved my problem to read the header but skip first row just after it (
pd.read_excel(path, skiprows=[1])
)