How to delete every 0.2-th row in a pandas dataframe?

df_o = pd.DataFrame(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o'])

I would like to continuously delete 4 consecutive rows but always spare the fifth in the dataframe above. The final result should be:

df_o = pd.DataFrame(['e', 'j', 'o'])

My idea df_o = df_o.drop(df_o.iloc[::0.2].index)does not work. It works well for deleting every n-th row if n is an integer, but not for my case.

>Solution :

Try this:

df_o.groupby(np.arange(len(df_o.index))//5).last()

Output:

   0
0  e
1  j
2  o

Leave a Reply