You can use the function head() to get the first k rows from a DataFrame. Similarly, the tail() function can get the last k rows from a DataFrame.You can also use iloc to select the first and last k rows.
Example:
>>> import numpy as np>>> import pandas as pd>>> df = pd.DataFrame({'a':np.random.random(5), 'b':np.random.random(5)})>>> df a b0 0.398201 0.9965881 0.687678 0.6096972 0.865939 0.3501593 0.037772 0.0004704 0.383119 0.181807>>> df.head(n=2) #first 2 rows a b0 0.398201 0.9965881 0.687678 0.609697>>> df.tail(n=2) #last 2 rows a b3 0.037772 0.0004704 0.383119 0.181807
Using iloc
>>> df.iloc[:2,:] #first 2 rows a b0 0.398201 0.9965881 0.687678 0.609697>>> df.iloc[-2:,:] #last 2 rows a b3 0.037772 0.0004704 0.383119 0.181807