Pandas DataFrame Count Method

PandasPandasBeginner
Practice Now

Introduction

In this lab, we will learn about the count() method in the Pandas DataFrame class. The count() method is used to count the number of non-null values in a DataFrame, either for each column or for each row.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL pandas(("`Pandas`")) -.-> pandas/DataSelectionGroup(["`Data Selection`"]) pandas(("`Pandas`")) -.-> pandas/DataAnalysisGroup(["`Data Analysis`"]) python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) pandas/DataSelectionGroup -.-> pandas/select_columns("`Select Columns`") pandas/DataAnalysisGroup -.-> pandas/basic_statistics("`Basic Statistics`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_analysis("`Data Analysis`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills pandas/select_columns -.-> lab-68601{{"`Pandas DataFrame Count Method`"}} pandas/basic_statistics -.-> lab-68601{{"`Pandas DataFrame Count Method`"}} python/booleans -.-> lab-68601{{"`Pandas DataFrame Count Method`"}} python/lists -.-> lab-68601{{"`Pandas DataFrame Count Method`"}} python/tuples -.-> lab-68601{{"`Pandas DataFrame Count Method`"}} python/importing_modules -.-> lab-68601{{"`Pandas DataFrame Count Method`"}} python/numerical_computing -.-> lab-68601{{"`Pandas DataFrame Count Method`"}} python/data_analysis -.-> lab-68601{{"`Pandas DataFrame Count Method`"}} python/build_in_functions -.-> lab-68601{{"`Pandas DataFrame Count Method`"}} end

Import the necessary libraries

First, let's import the necessary libraries for this lab:

import pandas as pd
import numpy as np

Create a DataFrame

Next, let's create a DataFrame that we will use for the examples in this lab. Our DataFrame will have information about students, such as their names, roll numbers, subjects, and marks.

df = pd.DataFrame([
    ['Abhishek', '101', 'Science', 90],
    ['Anurag', '102', None, 85]
], columns=['Name', 'Roll No', 'Subject', 'Marks'])

Count the non-null values for each column

To count the number of non-null values for each column in the DataFrame, we can use the count() method without any parameters. This will return a Series object where the index represents the column names and the values represent the count of non-null values in each column.

column_counts = df.count()
print(column_counts)

Output:

Name       2
Roll No    2
Subject    1
Marks      2
dtype: int64

Count the non-null values for each row

To count the number of non-null values for each row in the DataFrame, we can use the count() method with the axis parameter set to 1. This will return a Series object where the index represents the row numbers and the values represent the count of non-null values in each row.

row_counts = df.count(axis=1)
print(row_counts)

Output:

0    4
1    3
dtype: int64

Count the numeric non-null values for each row

If we want to count only the numeric non-null values for each row, we can use the count() method with the numeric_only parameter set to True. This will count only the columns with numeric data types (float, int) and exclude columns with non-numeric data types (object).

numeric_counts = df.count(numeric_only=True)
print(numeric_counts)

Output:

Roll No    2
Marks      2
dtype: int64

Summary

Congratulations! You have successfully completed this lab on the Pandas DataFrame count() method. In this lab, you learned how to count the number of non-null values in a DataFrame for each column or row using the count() method. You also learned how to count only the numeric non-null values using the numeric_only parameter. Keep practicing and exploring different methods of the Pandas library to become more proficient in data analysis and manipulation with Python. Happy coding!

Other Pandas Tutorials you may like