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.
CodePudding user response:
Try this:
df_o.groupby(np.arange(len(df_o.index))//5).last()
Output:
0
0 e
1 j
2 o
CodePudding user response:
You could use loc
:
import numpy as np
out = df_o.loc[np.arange(1, len(df_o) 1)%5==0, 0].tolist()
Output:
['e', 'j', 'o']
CodePudding user response:
Keep it simple: slice!
df_o.iloc[4::5]
Generalization:
N = 5
df_o.iloc[N-1::N]
Output:
0
4 e
9 j
14 o