Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete all rows at even indexes in a Dataframe? [duplicate]

I have dataframe pandas;

        Data
  Id    
ID_1    19
ID_2    33
ID_3    17
ID_4    52
ID_5    17
ID_6    41

Id is index. how to drop row by index but all of ID with even number?

this is my expected result:

        Data
  Id    
ID_1    19
ID_3    17
ID_5    17
like image 779
Arief Avatar asked Oct 18 '25 15:10

Arief


1 Answers

You can select each even row by DataFrame.iloc:

df1 = df.iloc[::2]

Or filter by mask created by numpy.arange with modulo 2 and compare by 0:

df1 = df[np.arange(len(df)) % 2 == 0]
print (df1)
      Data
Id        
ID_1    19
ID_3    17
ID_5    17
like image 67
jezrael Avatar answered Oct 21 '25 03:10

jezrael