{"id":4053,"date":"2023-10-25T19:45:54","date_gmt":"2023-10-25T14:15:54","guid":{"rendered":"https:\/\/python-programs.com\/?p=4053"},"modified":"2023-11-10T11:59:57","modified_gmt":"2023-11-10T06:29:57","slug":"pandas-count-rows-in-a-dataframe-all-or-those-only-that-satisfy-a-condition","status":"publish","type":"post","link":"https:\/\/python-programs.com\/pandas-count-rows-in-a-dataframe-all-or-those-only-that-satisfy-a-condition\/","title":{"rendered":"Pandas : count rows in a dataframe | all or those only that satisfy a condition"},"content":{"rendered":"

Count all rows or those that satisfy some condition in Pandas dataframe<\/h3>\n

In this article we are going to show you how to count number of all rows in a DataFrame or rows that satisfy given condition in it.<\/p>\n

First we are going to create dataframe,<\/p>\n

import pandas as pd\r\nstudents = [('Ankit', 22, 'Up', 'Geu'),\r\n           ('Ankita', 31, 'Delhi', 'Gehu'),\r\n           ('Rahul', 16, 'Tokyo', 'Abes'),\r\n           ('Simran', 41, 'Delhi', 'Gehu'),\r\n           ('Shaurya', 33, 'Delhi', 'Geu'),\r\n           ('Harshita', 35, 'Mumbai', 'Bhu' ),\r\n           ('Swapnil', 35, 'Mp', 'Geu'),\r\n           ('Priya', 35, 'Uk', 'Geu'),\r\n           ('Jeet', 35, 'Guj', 'Gehu'),\r\n           ('Ananya', 35, 'Up', 'Bhu')\r\n            ]\r\ndetails = pd.DataFrame(students, columns =['Name', 'Age','Place', 'College'],\r\n          index =['a', 'b', 'c', 'd', 'e','f', 'g', 'i', 'j', 'k'])\r\n\r\nprint(details)<\/pre>\n

Output:<\/strong><\/p>\n

RESTART: C:\/Users\/HP\/Desktop\/dataframe.py\r\nName\u00a0 \u00a0 \u00a0 \u00a0Age\u00a0 \u00a0Place\u00a0 \u00a0 \u00a0 College\r\na\u00a0 \u00a0Ankit\u00a0 \u00a0 \u00a0 \u00a0 \u00a022\u00a0 \u00a0 \u00a0Up\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Geu\r\nb\u00a0 \u00a0Ankita\u00a0 \u00a0 \u00a0 \u00a031\u00a0 \u00a0 Delhi\u00a0 \u00a0 \u00a0 \u00a0Gehu\r\nc\u00a0 \u00a0 Rahul\u00a0 \u00a0 \u00a0 \u00a016\u00a0 \u00a0 Tokyo\u00a0 \u00a0 \u00a0 Abes\r\nd\u00a0 \u00a0Simran\u00a0 \u00a0 \u00a041\u00a0 \u00a0 \u00a0Delhi\u00a0 \u00a0 \u00a0 Gehu\r\ne\u00a0 \u00a0Shaurya\u00a0 \u00a0 33\u00a0 \u00a0 \u00a0Delhi\u00a0 \u00a0 \u00a0 Geu\r\nf\u00a0 \u00a0 Harshita\u00a0 \u00a035\u00a0 \u00a0 \u00a0Mumbai Bhu\r\ng\u00a0 \u00a0Swapnil\u00a0 \u00a0 35\u00a0 \u00a0 \u00a0Mp\u00a0 \u00a0 \u00a0 \u00a0 Geu\r\ni\u00a0 \u00a0 Priya\u00a0 \u00a0 \u00a0 \u00a0 \u00a035\u00a0 \u00a0 \u00a0Uk\u00a0 \u00a0 \u00a0 \u00a0 \u00a0Geu\r\nj\u00a0 \u00a0 Jeet\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 35\u00a0 \u00a0 \u00a0Guj\u00a0 \u00a0 \u00a0 \u00a0 Gehu\r\nk\u00a0 \u00a0Ananya\u00a0 \u00a0 \u00a035\u00a0 \u00a0 Up\u00a0 \u00a0 \u00a0 \u00a0 \u00a0Bhu\r\n<\/pre>\n

Now lets see some other methods to count the rows in dataframe.<\/p>\n

Count all rows in a Pandas Dataframe using Dataframe.shape<\/h3>\n

Dataframe.shape attribute gives a sequence of index or row labels<\/p>\n

(Number_of_index, Number_of_columns)<\/em><\/p>\n

Number of index basically means number of rows in the dataframe.Let\u2019s use this to count number of rows in above created dataframe.<\/p>\n

import pandas as pd\r\nstudents = [('Ankit', 22, 'Up', 'Geu'),\r\n           ('Ankita', 31, 'Delhi', 'Gehu'),\r\n           ('Rahul', 16, 'Tokyo', 'Abes'),\r\n           ('Simran', 41, 'Delhi', 'Gehu'),\r\n           ('Shaurya', 33, 'Delhi', 'Geu'),\r\n           ('Harshita', 35, 'Mumbai', 'Bhu' ),\r\n           ('Swapnil', 35, 'Mp', 'Geu'),\r\n           ('Priya', 35, 'Uk', 'Geu'),\r\n           ('Jeet', 35, 'Guj', 'Gehu'),\r\n           ('Ananya', 35, 'Up', 'Bhu')\r\n            ]\r\ndetails = pd.DataFrame(students, columns =['Name', 'Age','Place', 'College'],\r\n          index =['a', 'b', 'c', 'd', 'e','f', 'g', 'i', 'j', 'k'])\r\nnumOfRows = details.shape[0]\r\n\r\nprint(\"Number of rows :\",numOfRows)<\/pre>\n

Output:<\/strong><\/p>\n

RESTART: C:\/Users\/HP\/Desktop\/dataframe.py\r\nNumber of rows : 10\r\n<\/pre>\n

Count all rows in a Pandas Dataframe using Dataframe.index<\/h3>\n

Dataframe.index\u00a0attribute gives a sequence of index or row labels.We can calculate the length of that sequence to find out the number of rows in the dataframe.<\/p>\n

import pandas as pd\r\nstudents = [('Ankit', 22, 'Up', 'Geu'),\r\n           ('Ankita', 31, 'Delhi', 'Gehu'),\r\n           ('Rahul', 16, 'Tokyo', 'Abes'),\r\n           ('Simran', 41, 'Delhi', 'Gehu'),\r\n           ('Shaurya', 33, 'Delhi', 'Geu'),\r\n           ('Harshita', 35, 'Mumbai', 'Bhu' ),\r\n           ('Swapnil', 35, 'Mp', 'Geu'),\r\n           ('Priya', 35, 'Uk', 'Geu'),\r\n           ('Jeet', 35, 'Guj', 'Gehu'),\r\n           ('Ananya', 35, 'Up', 'Bhu')\r\n            ]\r\ndetails = pd.DataFrame(students, columns =['Name', 'Age','Place', 'College'],\r\n          index =['a', 'b', 'c', 'd', 'e','f', 'g', 'i', 'j', 'k'])\r\nnumOfRows = len(details.index)\r\nprint('Number of Rows in dataframe : ' , numOfRows)<\/pre>\n

Output:<\/strong><\/p>\n

RESTART: C:\/Users\/HP\/Desktop\/dataframe.py\r\n\r\nNumber of rows : 10\r\n<\/pre>\n

Count rows in a Pandas Dataframe that satisfies a condition using Dataframe.apply()<\/h3>\n

This function apply\u00a0 to all the rows of a dataframe to find out if elements of rows satisfies a condition or not, Based on the result it returns a bool series.<\/p>\n

import pandas as pd\r\nstudents = [('Ankit', 22, 'Up', 'Geu'),\r\n           ('Ankita', 31, 'Delhi', 'Gehu'),\r\n           ('Rahul', 16, 'Tokyo', 'Abes'),\r\n           ('Simran', 41, 'Delhi', 'Gehu'),\r\n           ('Shaurya', 33, 'Delhi', 'Geu'),\r\n           ('Harshita', 35, 'Mumbai', 'Bhu' ),\r\n           ('Swapnil', 35, 'Mp', 'Geu'),\r\n           ('Priya', 35, 'Uk', 'Geu'),\r\n           ('Jeet', 35, 'Guj', 'Gehu'),\r\n           ('Ananya', 35, 'Up', 'Bhu')\r\n            ]\r\ndetails = pd.DataFrame(students, columns =['Name', 'Age','Place', 'College'],\r\n          index =['a', 'b', 'c', 'd', 'e','f', 'g', 'i', 'j', 'k'])\r\nseriesObj = details.apply(lambda x: True if x['Age'] > 30 else False , axis=1)\r\nnumOfRows = len(seriesObj[seriesObj == True].index)\r\nprint('Number of Rows in dataframe in which Age > 30 : ', numOfRows)<\/pre>\n

Output:<\/strong><\/p>\n

RESTART: C:\/Users\/HP\/Desktop\/dataframe.py\r\nNumber of Rows in dataframe in which Age > 30 : 8\r\n\r\n<\/pre>\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<\/strong><\/p>\n