Home > Software engineering >  Deleting rows from excel with Pandas
Deleting rows from excel with Pandas

Time:09-25

I'm trying to run the following code in Python. What I'm expecting is that the code will read in the Excel file, delete rows 1 and 2, then print the first few rows of data to the console:

import pandas as pd

path = 'C:\\Temp\\'  
filename = 'datafile1.xlsx' 
df = pd.read_excel(path   filename, sheet_name=1) 
df = df.drop([0,1])   #delete the first two rows
print(df.head()) 

I can't seem to upload the excel file here, so I've taken a screen shot of it here: [Excel file][1]

Here are the results that display in the console:

runfile('C:/Temp/getdata.py', wdir='C:/Temp')
           Title Here Unnamed: 1 Unnamed: 2 Unnamed: 3 Unnamed: 4 Unnamed: 5
NaN               NaN        NaN        NaN        NaN        NaN        NaN
sort order       Type  reference     cliref     couref      haref    contref
2                 FMN        NaN          b          5        dfs      dfs-5
3                 ACB        NaN          c          6        dfs      dfs-6
3                 ACB        NaN          d          7       fasf     fasf-7

The first two rows are still present: "Sort Order" should appear first, "Title Here" should not be showing at all.

What do I need to change? Thanks in advance for your help. [1]: file with simmilar struckture

you can always rename columns and header afterwards

  • Related