0

I have a data that contains 15 rows by 4 columns, does anyone know how to delete every rows except row number 5, 10 and 15?

Figure below shows the input data, the whole highlighted rows would be the rows that I want to retain, in other words, I would like to delete row from 1 to 4, 6 to 9 and 11 to 14. Any help would be appreciated a lot, thanks in advance!

enter image description here

Edison Toh
  • 87
  • 1
  • 11

1 Answers1

0

Use iloc to selection by position

df = pd.read_excel('d.xlsx',header=None)        # read file
df = df.iloc[[4,9,14]]                          # filter rows
df
     0  1   2  3
4    5  e   5  e
9   10  j  10  j
14   5  e   5  e
df.to_excel('d.xlsx',index=None,header=None)     # save file

Update
As per comment: To remove every 5th row from dataframe. Use DataFrame.drop with slicing. df.drop(df.index[4::5])

df = pd.read_excel('d.xlsx',header=None)         # read file
df = df.drop(df.index[4::5])                     # drop every 5th rows
df.to_excel('d.xlsx',index=None,header=None)     # save file
Dishin H Goyani
  • 7,195
  • 3
  • 26
  • 37
  • Hi, thanks for your answer, is it possible to make loop for it? Because the actual data is quite large like thousand rows, could every 5th be deleted? Such as row of 5, 10, 15, 20, 25, 30 and etc.. – Edison Toh Mar 19 '20 at 13:35
  • @EdisonToh please check Updated answer. – Dishin H Goyani Mar 20 '20 at 03:58