22

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()
DYZ
  • 55,249
  • 10
  • 64
  • 93
Ajml
  • 379
  • 1
  • 2
  • 13
  • 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. – AChampion Aug 26 '17 at 06:46
  • just `skiprows()` will do for me, thank you. – Ajml Aug 26 '17 at 06:47

3 Answers3

45

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

Onel Harrison
  • 1,244
  • 12
  • 14
  • Hi @Onel Harrison, could you help with this question? https://stackoverflow.com/questions/46751996/selecting-and-importing-only-certain-columns-from-excel-for-importing – Ajml Oct 16 '17 at 03:41
  • 2
    And if you want to skip first two rows then; `pd.read_excel("file.xlsx", parse_cols="A,C,E,G", skiprows=[0,1])` – msklc May 17 '22 at 14:46
  • 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) – pfrank Dec 20 '22 at 17:59
2

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

Max
  • 1,283
  • 9
  • 20
  • 3
    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 data – AChampion Aug 26 '17 at 06:51
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.

BoB
  • 129
  • 2
  • 9