I have a dataframe such as :
COL1 COL2 COL3
A B C
D E F
G G I
J K L
M N O
P Q R
S T U
V W X
Y Z A
and I would like to keep only intervals of 3 rows. I should then get :
COL1 COL2 COL3
G G I
P Q R
Y Z A
Does someone have an idea?
CodePudding user response:
You can use .iloc
and range:
df.iloc[range(2,len(df),3)]
CodePudding user response:
Wouldn't a slicing do the job?:
df[2::3]
CodePudding user response:
The iloc
property allows you to access slices of a dataframe similarly to a 2d numpy array, of a 1d list/tuple if slicing along the index (first axis).
In your case, as @QuangHoang answered:
df.iloc[2::3]
CodePudding user response:
Try groupby
out = df.groupby(df.index//3).tail(1)
Out[446]:
COL1 COL2 COL3
2 G G I
5 P Q R
8 Y Z A