{"id":6093,"date":"2023-10-29T16:56:29","date_gmt":"2023-10-29T11:26:29","guid":{"rendered":"https:\/\/python-programs.com\/?p=6093"},"modified":"2023-11-10T12:06:42","modified_gmt":"2023-11-10T06:36:42","slug":"pandas-select-first-or-last-n-rows-in-a-dataframe-using-head-tail","status":"publish","type":"post","link":"https:\/\/python-programs.com\/pandas-select-first-or-last-n-rows-in-a-dataframe-using-head-tail\/","title":{"rendered":"Pandas: Select first or last N rows in a Dataframe using head() & tail()"},"content":{"rendered":"
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.<\/p>\n
In Python’s Pandas module, the Dataframe class gives the head() function to fetch top rows from it.<\/p>\n
Syntax:<\/strong><\/p>\n If we give some value to n it will return n number of rows otherwise default is 5.<\/p>\n Let’s create a dataframe first,<\/p>\n Output:<\/strong><\/p>\n So if we want to select the top 4 rows from the dataframe,<\/p>\n Output:<\/strong><\/p>\n 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.<\/p>\n Do Check:<\/span><\/p>\n In this, while selecting the first 3 rows, we can select specific columns too,<\/p>\n Output:<\/strong><\/p>\n In the Pandas module, the Dataframe class provides a tail() function to select bottom rows from a Dataframe.<\/p>\n Syntax:<\/p>\n 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,<\/p>\n Output:<\/strong><\/p>\n So in above example, you can see that we are given n value 4 so tail() function return last 4 data value.<\/p>\n In this, while selecting the last 4 rows, we can select specific columns too,<\/p>\n Output:<\/strong><\/p>\n In this article, you have seen how to select first or last N\u00a0 rows in a Dataframe using head() & tail() functions. Thank you!<\/p>\n Want to expert in the python programming language? Exploring\u00a0Python Data Analysis using Pandas<\/a>\u00a0tutorial changes your knowledge from basic to advance level in python concepts.<\/p>\n Read more Articles on Python Data Analysis Using Padas \u2013 Select items from a Dataframe<\/strong><\/p>\n 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 …<\/p>\nDataFrame.head(self, n=5)<\/code><\/p>\n
import pandas as pd\r\n# List of Tuples\r\nempoyees = [('Ram', 34, 'Sunderpur', 5) ,\r\n ('Riti', 31, 'Delhi' , 7) ,\r\n ('Aman', 16, 'Thane', 9) ,\r\n ('Shishir', 41,'Delhi' , 12) ,\r\n ('Veeru', 33, 'Delhi' , 4) ,\r\n ('Shan',35,'Mumbai', 5 ),\r\n ('Shikha', 35, 'kolkata', 11)\r\n ]\r\n# Create a DataFrame object\r\nempDfObj = pd.DataFrame(empoyees, columns=['Name', 'Age', 'City', 'Experience'], index=['a', 'b', 'c', 'd', 'e', 'f', 'g'])\r\nprint(\"Contents of the Dataframe : \")\r\nprint(empDfObj)<\/pre>\n
Contents of the Dataframe :\r\n Name Age City Experience\r\na Ram 34 Sunderpur 5\r\nb Riti 31 Delhi 7\r\nc Aman 16 Thane 9\r\nd Shishir 41 Delhi 12\r\ne Veeru 33 Delhi 4\r\nf Shan 35 Mumbai 5\r\ng Shikha 35 kolkata 11\r\n\r\n<\/pre>\n
import pandas as pd\r\n# List of Tuples\r\nempoyees = [('Ram', 34, 'Sunderpur', 5) ,\r\n ('Riti', 31, 'Delhi' , 7) ,\r\n ('Aman', 16, 'Thane', 9) ,\r\n ('Shishir', 41,'Delhi' , 12) ,\r\n ('Veeru', 33, 'Delhi' , 4) ,\r\n ('Shan',35,'Mumbai', 5 ),\r\n ('Shikha', 35, 'kolkata', 11)\r\n ]\r\n# Create a DataFrame object\r\nempDfObj = pd.DataFrame(empoyees, columns=['Name', 'Age', 'City', 'Experience'], index=['a', 'b', 'c', 'd', 'e', 'f', 'g'])\r\n\r\ndfObj1 = empDfObj.head(4)\r\nprint(\"First 4 rows of the Dataframe : \")\r\nprint(dfObj1)<\/pre>\n
First 4 rows of the Dataframe :\r\n Name Age City Experience\r\na Ram 34 Sunderpur 5\r\nb Riti 31 Delhi 7\r\nc Aman 16 Thane 9\r\nd Shishir 41 Delhi 12\r\n\r\n<\/pre>\n
\n
<\/a>Select first N rows from the dataframe with specific columns<\/h3>\n
import pandas as pd\r\n# List of Tuples\r\nempoyees = [('Ram', 34, 'Sunderpur', 5) ,\r\n ('Riti', 31, 'Delhi' , 7) ,\r\n ('Aman', 16, 'Thane', 9) ,\r\n ('Shishir', 41,'Delhi' , 12) ,\r\n ('Veeru', 33, 'Delhi' , 4) ,\r\n ('Shan',35,'Mumbai', 5 ),\r\n ('Shikha', 35, 'kolkata', 11)\r\n ]\r\n# Create a DataFrame object\r\nempDfObj = pd.DataFrame(empoyees, columns=['Name', 'Age', 'City', 'Experience'], index=['a', 'b', 'c', 'd', 'e', 'f', 'g'])\r\n\r\n# Select the top 3 rows of the Dataframe for 2 columns only\r\ndfObj1 = empDfObj[['Name', 'City']].head(3)\r\nprint(\"First 3 rows of the Dataframe for 2 columns : \")\r\nprint(dfObj1)\r\n<\/pre>\n
First 3 rows of the Dataframe for 2 columns :\r\n Name City\r\na Ram Sunderpur\r\nb Riti Delhi\r\nc Aman Thane\r\n<\/pre>\n
<\/a>Select last N Rows from a Dataframe using tail() function<\/h3>\n
DataFrame.tail(self, n=5)<\/code><\/p>\n
import pandas as pd\r\n# List of Tuples\r\nempoyees = [('Ram', 34, 'Sunderpur', 5) ,\r\n ('Riti', 31, 'Delhi' , 7) ,\r\n ('Aman', 16, 'Thane', 9) ,\r\n ('Shishir', 41,'Delhi' , 12) ,\r\n ('Veeru', 33, 'Delhi' , 4) ,\r\n ('Shan',35,'Mumbai', 5 ),\r\n ('Shikha', 35, 'kolkata', 11)\r\n ]\r\n# Create a DataFrame object\r\nempDfObj = pd.DataFrame(empoyees, columns=['Name', 'Age', 'City', 'Experience'], index=['a', 'b', 'c', 'd', 'e', 'f', 'g'])\r\n\r\n# Select the last 4 rows of the Dataframe\r\ndfObj1 = empDfObj.tail(4)\r\nprint(\"Last 4 rows of the Dataframe : \")\r\nprint(dfObj1)<\/pre>\n
Last 5 rows of the Dataframe :\r\n Name Age City Experience\r\nd Shishir 41 Delhi 12\r\ne Veeru 33 Delhi 4\r\nf Shan 35 Mumbai 5\r\ng Shikha 35 kolkata 11\r\n\r\n<\/pre>\n
<\/a>Select bottom N rows from the dataframe with specific columns<\/h3>\n
import pandas as pd\r\n# List of Tuples\r\nempoyees = [('Ram', 34, 'Sunderpur', 5) ,\r\n ('Riti', 31, 'Delhi' , 7) ,\r\n ('Aman', 16, 'Thane', 9) ,\r\n ('Shishir', 41,'Delhi' , 12) ,\r\n ('Veeru', 33, 'Delhi' , 4) ,\r\n ('Shan',35,'Mumbai', 5 ),\r\n ('Shikha', 35, 'kolkata', 11)\r\n ]\r\n# Create a DataFrame object\r\nempDfObj = pd.DataFrame(empoyees, columns=['Name', 'Age', 'City', 'Experience'], index=['a', 'b', 'c', 'd', 'e', 'f', 'g'])\r\n\r\n# Select the bottom 4 rows of the Dataframe for 2 columns only\r\ndfObj1 = empDfObj[['Name', 'City']].tail(4)\r\nprint(\"Last 4 rows of the Dataframe for 2 columns : \")\r\nprint(dfObj1)\r\n<\/pre>\n
Last 4 rows of the Dataframe for 2 columns :\r\n Name City\r\nd\u00a0 Shishir\u00a0 Delhi\r\ne\u00a0 Veeru\u00a0 \u00a0 Delhi\r\nf\u00a0 \u00a0Shan\u00a0 \u00a0 \u00a0Mumbai\r\ng\u00a0 Shikha\u00a0 \u00a0kolkata\r\n<\/pre>\n
Conclusion:<\/h3>\n
\n