Home > Back-end >  I'm using pandas to delete first 9 rows but it is still keeping the first row
I'm using pandas to delete first 9 rows but it is still keeping the first row

Time:08-13

Below is my code -
import pandas as pd
import datetime

df1 = pd.read_excel(str(sys_folder)   "Italy_SS304.xlsx")
df1.drop(df1.index[0:9], axis=0, inplace=True)
df1.drop(df1.columns[1:3], axis=1, inplace=True)

df1

attached image is my database from excel

CodePudding user response:

While reading the excel, the first row is taken as header, unless you explicitly say it is not. You need to add header = False so that the dataframe does not take the first row as header.

Assuming you are writing df1 back into an excel using something like df1.to_excel(), you will need to use the same header=None, index=False, assuming you don't want to add index.

Change the read_excel file to as shown below.

df1 = pd.read_excel(str(sys_folder)   "Italy_SS304.xlsx", header=None)

...and if you are writing back to excel, use the line like this (after the drop commands)

df1.to_excel('NEWFILE.xlsx', header=None, index=False)
  • Related