In this tutorial, we are going to discuss how to select the first or last N rows in a Dataframe using head() & tail() functions. This guide describes the following contents.
- Select first N Rows from a Dataframe using head() function
- Select first N rows from the dataframe with specific columns
- Select last N Rows from a Dataframe using tail() function
- Select bottom N rows from the dataframe with specific columns
Select first N Rows from a Dataframe using head() function
pandas.DataFrame.head()
In Python’s Pandas module, the Dataframe class gives the head() function to fetch top rows from it.
Syntax:
DataFrame.head(self, n=5)
If we give some value to n it will return n number of rows otherwise default is 5.
Let’s create a dataframe first,
import pandas as pd # List of Tuples empoyees = [('Ram', 34, 'Sunderpur', 5) , ('Riti', 31, 'Delhi' , 7) , ('Aman', 16, 'Thane', 9) , ('Shishir', 41,'Delhi' , 12) , ('Veeru', 33, 'Delhi' , 4) , ('Shan',35,'Mumbai', 5 ), ('Shikha', 35, 'kolkata', 11) ] # Create a DataFrame object empDfObj = pd.DataFrame(empoyees, columns=['Name', 'Age', 'City', 'Experience'], index=['a', 'b', 'c', 'd', 'e', 'f', 'g']) print("Contents of the Dataframe : ") print(empDfObj)
Output:
Contents of the Dataframe : Name Age City Experience a Ram 34 Sunderpur 5 b Riti 31 Delhi 7 c Aman 16 Thane 9 d Shishir 41 Delhi 12 e Veeru 33 Delhi 4 f Shan 35 Mumbai 5 g Shikha 35 kolkata 11
So if we want to select the top 4 rows from the dataframe,
import pandas as pd # List of Tuples empoyees = [('Ram', 34, 'Sunderpur', 5) , ('Riti', 31, 'Delhi' , 7) , ('Aman', 16, 'Thane', 9) , ('Shishir', 41,'Delhi' , 12) , ('Veeru', 33, 'Delhi' , 4) , ('Shan',35,'Mumbai', 5 ), ('Shikha', 35, 'kolkata', 11) ] # Create a DataFrame object empDfObj = pd.DataFrame(empoyees, columns=['Name', 'Age', 'City', 'Experience'], index=['a', 'b', 'c', 'd', 'e', 'f', 'g']) dfObj1 = empDfObj.head(4) print("First 4 rows of the Dataframe : ") print(dfObj1)
Output:
First 4 rows of the Dataframe : Name Age City Experience a Ram 34 Sunderpur 5 b Riti 31 Delhi 7 c Aman 16 Thane 9 d Shishir 41 Delhi 12
So in the above example, you can see that we have given n value 4 so it returned the top 4 rows from the dataframe.
Do Check:
- Python: numpy.flatten() – Function Tutorial with examples
- Python: Add column to dataframe in Pandas ( based on other column or list or default value)
Select first N rows from the dataframe with specific columns
In this, while selecting the first 3 rows, we can select specific columns too,
import pandas as pd # List of Tuples empoyees = [('Ram', 34, 'Sunderpur', 5) , ('Riti', 31, 'Delhi' , 7) , ('Aman', 16, 'Thane', 9) , ('Shishir', 41,'Delhi' , 12) , ('Veeru', 33, 'Delhi' , 4) , ('Shan',35,'Mumbai', 5 ), ('Shikha', 35, 'kolkata', 11) ] # Create a DataFrame object empDfObj = pd.DataFrame(empoyees, columns=['Name', 'Age', 'City', 'Experience'], index=['a', 'b', 'c', 'd', 'e', 'f', 'g']) # Select the top 3 rows of the Dataframe for 2 columns only dfObj1 = empDfObj[['Name', 'City']].head(3) print("First 3 rows of the Dataframe for 2 columns : ") print(dfObj1)
Output:
First 3 rows of the Dataframe for 2 columns : Name City a Ram Sunderpur b Riti Delhi c Aman Thane
Select last N Rows from a Dataframe using tail() function
In the Pandas module, the Dataframe class provides a tail() function to select bottom rows from a Dataframe.
Syntax:
DataFrame.tail(self, n=5)
It will return the last n rows from a dataframe. If n is not provided then the default value is 5. So for this, we are going to use the above dataframe as an example,
import pandas as pd # List of Tuples empoyees = [('Ram', 34, 'Sunderpur', 5) , ('Riti', 31, 'Delhi' , 7) , ('Aman', 16, 'Thane', 9) , ('Shishir', 41,'Delhi' , 12) , ('Veeru', 33, 'Delhi' , 4) , ('Shan',35,'Mumbai', 5 ), ('Shikha', 35, 'kolkata', 11) ] # Create a DataFrame object empDfObj = pd.DataFrame(empoyees, columns=['Name', 'Age', 'City', 'Experience'], index=['a', 'b', 'c', 'd', 'e', 'f', 'g']) # Select the last 4 rows of the Dataframe dfObj1 = empDfObj.tail(4) print("Last 4 rows of the Dataframe : ") print(dfObj1)
Output:
Last 5 rows of the Dataframe : Name Age City Experience d Shishir 41 Delhi 12 e Veeru 33 Delhi 4 f Shan 35 Mumbai 5 g Shikha 35 kolkata 11
So in above example, you can see that we are given n value 4 so tail() function return last 4 data value.
Select bottom N rows from the dataframe with specific columns
In this, while selecting the last 4 rows, we can select specific columns too,
import pandas as pd # List of Tuples empoyees = [('Ram', 34, 'Sunderpur', 5) , ('Riti', 31, 'Delhi' , 7) , ('Aman', 16, 'Thane', 9) , ('Shishir', 41,'Delhi' , 12) , ('Veeru', 33, 'Delhi' , 4) , ('Shan',35,'Mumbai', 5 ), ('Shikha', 35, 'kolkata', 11) ] # Create a DataFrame object empDfObj = pd.DataFrame(empoyees, columns=['Name', 'Age', 'City', 'Experience'], index=['a', 'b', 'c', 'd', 'e', 'f', 'g']) # Select the bottom 4 rows of the Dataframe for 2 columns only dfObj1 = empDfObj[['Name', 'City']].tail(4) print("Last 4 rows of the Dataframe for 2 columns : ") print(dfObj1)
Output:
Last 4 rows of the Dataframe for 2 columns : Name City d Shishir Delhi e Veeru  Delhi f  Shan   Mumbai g Shikha  kolkata
Conclusion:
In this article, you have seen how to select first or last NÂ rows in a Dataframe using head() & tail() functions. Thank you!
Want to expert in the python programming language? Exploring Python Data Analysis using Pandas tutorial changes your knowledge from basic to advance level in python concepts.
Read more Articles on Python Data Analysis Using Padas – Select items from a Dataframe
- Select Rows & Columns in a Dataframe using loc & iloc in
- Select Rows in a Dataframe based on conditions
- Get minimum values in rows or columns & their index position in Dataframe
- Get unique values in columns of a Dataframe
- Get a list of column and row names in a DataFrame
- Get DataFrame contents as a list of rows or columns (list of lists)