Exclude first row when importing data from excel into Python
Asked Answered
B

3

23

I have a partial code to import excel into Python as strings. How I can exclude first row when importing data from excel into Python?

import pandas as pd
data = pd.read_excel(".xlsx", parse_cols="A,C,E,G, I, K, M, O, Q, S, U, W, Y, AA, AC, AE, AG, AI, AK, AM, AO, AQ, AS, AU, AW, AY, BA, BC, BE, BG, BI, BK, BM, BO, BQ, BS, BU, BW, BY, CA, CC, CE, CG, CI, CK, CM, CO, CQ, CS, CU, CW, CY, DA, DC, DE, DG, DI, DK, DM, DO, DQ, DS, DU, DW, DY, EA, EC, DE, EG, EI, EK, EM, EO, EQ, ES, EU, EW, EY")
data = data.to_string()
Bolo answered 26/8, 2017 at 6:33 Comment(2)
Are you looking to avoid the headers in data? Or do you have unnecessary rows in the excel. Using skiprows() to avoid reading in the header is probably not the right approach. You can do: data.to_string(header=False) to avoid the headers.Letterhead
just skiprows() will do for me, thank you.Bolo
A
51

The pandas documentation for the pd.read_excel method mentions a skiprows parameter that you can use to exclude the first row of your excel file.

Example

import pandas as pd
data = pd.read_excel("file.xlsx", parse_cols="A,C,E,G", skiprows=[0])

Source: pandas docs

Argil answered 26/8, 2017 at 6:40 Comment(3)
Hi @Onel Harrison, could you help with this question? #46752496Bolo
And if you want to skip first two rows then; pd.read_excel("file.xlsx", parse_cols="A,C,E,G", skiprows=[0,1])Gyrostatics
Might be a version issue, but this worked for me instead of using an array: data = pd.read_excel("file.xlsx", parse_cols="A,C,E,G", skiprows=1)Gastrostomy
M
2

for read_excel function assign value to skiprows argument. it will ignore the header

Mindexpanding answered 26/8, 2017 at 6:35 Comment(1)
Note: skiprows doesn't mean that a header is not created from the first non-skipped row, header=None would avoid creating a header from the dataLetterhead
T
0

parse_cols argument is deprecated since version 0.21.0. Instead you should use usecols:

usecols : int or list, default None

  • If None then parse all columns, If int then indicates last column to
  • be parsed If list of ints then indicates list of column numbers to be
  • parsed If string then indicates comma separated list of Excel column
  • letters and column ranges (e.g. “A:E” or “A,C,E:F”). Ranges are
  • inclusive of both sides.

To exclude first row use skiprows=[0] argument.

Tendon answered 17/10, 2018 at 9:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.