Pandas select n middle rows

Viewed 33534

Suppose I have a dataframe, df like this

col1 col2 col3
 1     2     34
 11    32    32
 21    62    34
 31    12    31
 13    82    35
 11    32    33
 41    32    33

and I want to select 3 rows after first 2 rows, that is I want to select these rows

 21    62    34
 31    12    31
 13    82    35

How can I do this?

2 Answers

Use slicing of rows with loc to do that like df.loc[2:5]

Output:

  col1  col2  col3
2    21    62    34
3    31    12    31
4    13    82    35
5    11    32    33

If you want to ignore the current index then use slicing with iloc which will get the rows between the range.

df.iloc[2:4]
  col1  col2  col3
2    21    62    34
3    31    12    31

You can do df.iloc[2:4] or just df[2:4].

Related