2

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?

chippycentra
  • 3,396
  • 1
  • 6
  • 24

4 Answers4

4

You can use .iloc and range:

df.iloc[range(2,len(df),3)]
4

Wouldn't a slicing do the job?:

df[2::3]
nikeros
  • 3,302
  • 2
  • 10
  • 26
4

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]
Learning is a mess
  • 7,479
  • 7
  • 35
  • 71
2

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
BENY
  • 317,841
  • 20
  • 164
  • 234