Python Forum
How can I skip the first row in the dataframe?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I skip the first row in the dataframe?
#1
Hello,

The Excel file first row is header line. If skiprows=0 in the following line, is the header line becomes the first data row in the dataframe?
df_Excel = pd.read_excel(filepath, sheet_name="Sheet1", skiprows=0, dtype=str, engine="openpyxl")

If so, how can I delete the header line from the dataframe later?

I need the header line to merge with another dataframe, so I can't have skiprows=1 in pd.read_excel.

Thanks.
Dee
Reply
#2
set skiprows=1 to skip header
Reply
#3
I don't think you can delete the column names of a pandas dataframe. You can rename them.

Why would you want to delete the header line? You need some way of referring to the columns! Better a meaningful name!

The headers row is not part of the data, just names for the columns.

Your data begins with the second row of your XL file. In the df the second row of the XL is default index row zero, 0.

import pandas as pd

data = '/home/peterr/myPython/pandas/xl_files/mexico.xlsx'

# default behaviour is to use the first row of the XL as headers
df = pd.read_excel(data) # no need to delete the column headers
df = pd.read_excel(data, skiprows=1) # now pandas uses row 2 of the XL as headers
# use custom column names
cols = ['col1', 'col2', 'col3']
df = pd.read_excel(data,  names=cols) # now pandas skips row 1 of the XL and uses cols as column names
To delete rows in a pandas dataframe, look up " pandas delete row"
Reply
#4
Hello,

if the first row in an Excel file is a header, you actually don't need to do anything. The default for Panda's read_excel function is using header=0 (see documentation), which takes automatically the first row as the header. Whether you use the header or not is up to you, but each columns needs some type of label. skiprows is typically used for explicitly skipping rows of data.

In case you ever have an Excel file with no headers, use header=None, otherwise the first data row is used a a header. To ignore the first row of headers explicitly although it contains headers and start using data with the 2nd row, you may want to try header=None, skiprows=0 as arguments to read_excel.

Regards, noisefloor
buran likes this post
Reply
#5
A pandas dataframe must have column headers, so we can refer to the columns.

Don't you think it would be better to keep the meaningful headers from the XL, instead of just having numbers as column headers?

If I do this (don't use skiprows=0, or you will get the XL column headers as data):

df = pd.read_excel(data, skiprows=1, header=None) # now the column headers are numbers and we just have the data
I have omitted the XL headers and just have numbers as column headers, but you cannot avoid having column headers:

Output:
0 1 2 0 93512 Chk1 Mexico 1 681593 Chk2 Mexico 2 331574 Chk3 Mexico 3 89153 Chk4 Mexico
Reply
#6
Thanks Pedroski55 and noisefloor to confirm that the headers row is not the first row of the data, just names for the columns.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  need to skip password prompt, continue... tester_V 2 2,650 Oct-19-2021, 05:49 PM
Last Post: tester_V
  Delimiters - How to skip some html tags from being translate Melcu54 0 2,600 May-26-2021, 06:21 AM
Last Post: Melcu54
  How to skip to a different line while importing a different script kat_gamer 2 3,831 Feb-03-2021, 04:10 AM
Last Post: deanhystad
  How to skip a folder directory in a loop mfkzolo 2 20,256 Nov-18-2020, 07:56 AM
Last Post: mfkzolo
  How to skip LinkedIn signup link using python script? Mangesh121 0 2,732 Aug-26-2020, 01:22 PM
Last Post: Mangesh121
  How to calculate column mean and row skip non numeric and na Mekala 5 8,841 May-06-2020, 10:52 AM
Last Post: anbu23
  Functions, skip an arugment SpongeB0B 2 3,615 Mar-27-2020, 12:10 PM
Last Post: SpongeB0B
  Skip a line doug2019 4 4,838 Oct-09-2019, 06:56 PM
Last Post: ichabod801
  Skip header python_bg1 1 3,254 Jul-25-2019, 01:34 AM
Last Post: ichabod801
  Python: why skip the 'else' if password is wrong Max_988 1 2,993 Jun-20-2019, 12:19 AM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020