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?
Method 1: Using tail() method Use pandas. DataFrame. tail(n) to get the last n rows of the DataFrame. It takes one optional argument n (number of rows you want to get from the end).
You can use df. head() to get the first N rows in Pandas DataFrame. Alternatively, you can specify a negative number within the brackets to get all the rows, excluding the last N rows.
The head() method returns a specified number of rows, string from the top. The head() method returns the first 5 rows if a number is not specified. Note: The column names will also be returned, in addition to the specified rows.
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]
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With