Count Values in Pandas Dataframe - GeeksforGeeks

09 Aug, 2021
In this article, we are going to count values in Pandas dataframe. First, we will create a data frame, and then we will count the values of different attributes.
Syntax: DataFrame.count(axis=0, level=None, numeric_only=False)
Parameters:
axis {0 or ‘index’, 1 or ‘columns’}: default 0 Counts are generated for each column if axis=0 or axis=’index’ and counts are generated for each row if axis=1 or axis=”columns”.
level (nt or str, optional): If the axis is a MultiIndex, count along a particular level, collapsing into a DataFrame. A str specifies the level name.
numeric_only (boolean, default False): It includes only int, float or boolean value.
Returns: It returns count of non-null values and if level is used it returns dataframe
Step-by-step approach:
Step 1: Importing libraries.
Python3
# importing librariesimport numpy as npimport pandas as pd
Step 2: Creating Dataframe
Python3
# Creating dataframe with# some missing valuesNaN = np.nandataframe = pd.DataFrame({'Name': ['Shobhit', 'Vaibhav',                                   'Vimal', 'Sourabh',                                   'Rahul', 'Shobhit'],                          'Physics': [11, 12, 13, 14, NaN, 11],                          'Chemistry': [10, 14, NaN, 18, 20, 10],                          'Math': [13, 10, 15, NaN, NaN, 13]}) display(dataframe)
Output:
Created Dataframe
Step 3: In this step, we just simply use the .count() function to count all the values of different columns.
Python3
# using dataframe.count()# to count all valuesdataframe.count()
Output:
We can see that there is a difference in count value as we have missing values. There are 5 values in the Name column,4 in Physics and Chemistry, and 3 in Math. In this case, it uses it’s an argument with its default values.
Step 4: If we want to count all the values with respect to row then we have to pass axis=1 or ‘columns’.
Python3
# we can pass either axis=1 or# axos='columns' to count with respect to rowprint(dataframe.count(axis = 1)) print(dataframe.count(axis = 'columns'))
Output:
count with respect to row
Step 5: Now if we want to count null values in our dataframe.
Python3
# it will give the count# of individual columns count of null valuesprint(dataframe.isnull().sum()) # it will give the total null# values present in our dataframeprint("Total Null values count: ",      dataframe.isnull().sum().sum())
Output:
Step 6:. Some examples to use .count()
Now we want to count no of students whose physics marks are greater than 11.
Python3
# count of student with greater# than 11 marks in physicsprint("Count of students with physics marks greater than 11 is->",      dataframe[dataframe['Physics'] > 11]['Name'].count()) # resultant of above dataframedataframe[dataframe['Physics']>11]
Output:
Physics>11
Count of students whose physics marks are greater than 10,chemistry marks are greater than 11 and math marks are greater than 9.
Python3
# Count of students whose physics marks# are greater than 10,chemistry marks are# greater than 11 and math marks are greater than 9.print("Count of students ->",      dataframe[(dataframe['Physics'] > 10) &                (dataframe['Chemistry'] > 11) &                (dataframe['Math'] > 9)]['Name'].count()) # dataframe of above resultdataframe[(dataframe['Physics'] > 10 ) &          (dataframe['Chemistry'] > 11 ) &          (dataframe['Math'] > 9 )]
Output:
Physics>10 ,Chemistry>11,Maths>9
Below is the full implementation:
Python3
# importing Librariesimport pandas as pdimport numpy as np # Creating dataframe using dictionaryNaN = np.nandataframe = pd.DataFrame({'Name': ['Shobhit', 'Vaibhav',                                   'Vimal', 'Sourabh',                                   'Rahul', 'Shobhit'],                          'Physics': [11, 12, 13, 14, NaN, 11],                          'Chemistry': [10, 14, NaN, 18, 20, 10],                          'Math': [13, 10, 15, NaN, NaN, 13]}) print("Created Dataframe")print(dataframe) # finding Count of all columnsprint("Count of all values wrt columns")print(dataframe.count()) # Count according to rowsprint("Count of all values wrt rows")print(dataframe.count(axis=1))print(dataframe.count(axis='columns')) # count of null valuesprint("Null Values counts ")print(dataframe.isnull().sum())print("Total null values",      dataframe.isnull().sum().sum()) # count of student with greater# than 11 marks in physicsprint("Count of students with physics marks greater than 11 is->",      dataframe[dataframe['Physics'] > 11]['Name'].count()) # resultant of above dataframeprint(dataframe[dataframe['Physics'] > 11])print("Count of students ->",      dataframe[(dataframe['Physics'] > 10) &                (dataframe['Chemistry'] > 11) &                (dataframe['Math'] > 9)]['Name'].count()) print(dataframe[(dataframe['Physics'] > 10) &                (dataframe['Chemistry'] > 11) &                (dataframe['Math'] > 9)])
Output:
akshaysingh98088
Python pandas-dataFrame
Python Pandas-exercise
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
Selecting rows in pandas DataFrame based on conditions
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Get unique values from a list
Defaultdict in Python
Python OOPs Concepts
Python | os.path.join() method
Python | Pandas dataframe.groupby()