pandas Count

The great ecosystem of information-driven Python packages is a significant factor in why Python is a fantastic platform for conducting data research. One such package, pandas, simplifies taking in and analyzing data.

Missing values might be problematic in some circumstances. Thus, we occasionally need to specify objects with non-missing values. One method for locating columns with a lot of missing data is to use the pandas count method.

The pandas count() function is a method for calculating the number of non-NA cells in each segment or column. Moreover, working with non-skimming data is also relevant. When dealing with datasets, a tremendous skill is the ability to present outcomes understandably. Using an axis-based graph is a common way to display data. The Python function count() returns the number of times the substring appears in the string and the number of values in columns or rows of a DataFrame. We will thus go over how to utilize the count function on DataFrames in this part.

Syntax for the pandas count() Function

The count method has a relatively straightforward syntax; however, there are a few different approaches to utilize it and some options that can change how it works. You only need to specify the DataFrame’s name followed by “.count()” to invoke the count function for a dataframe. Thus, assuming your dataframe is called “Dataframe,” you could utilize the script “Dataframe.count()” to determine the amount of non-missing entries for all of the columns. Inside the brackets, you could also utilize a few optional arguments that we’ll explain in a bit.

Here, the “level” denotes the axis’s various indexing, and if the axis is hierarchical, the DataFrame’s count() method eventually crashes and stops responding to program calls, leaving the program hanging. The term “numeric” refers to the program’s compatibility with numeric data, including integer, float, and Logic values. Since it must always return to the dataframe when the level is provided, it takes the false value as a default. The program’s assessment of the rows and columns is provided on the “axis”. The count() method uses the axis argument to specify specific columns and rows to take into account whenever the result is to be produced by the application utilizing pandas.

After examining the syntax, let’s look at some demonstrations of the pandas count approach in practice. We’ll explore some instances of ways to count the values inside a dataframe, count the entries in a particular column, and some further applications.

Example 1: Count the Number of Records in All Columns of a DataFrame Using the pandas count() Method

You’ll be required to execute some preparatory code before you can compile all instances. We must import the relevant libraries and then load/create a dataframe, specifically.

First, we import the NumPy library as np and pandas library and give it the name pd in the previous program. We can now start constructing our fundamental DataFrame as we get accessibility to the pandas library.

Beginning with the main code, here you can see that we have used an np.nan property and made it equal to NaN. The acronym NaN, which refers to “Not a Number,” denotes numbers that are not stated. Additionally, missing entries in a dataset are represented using it.

Now, we will construct a DataFrame with some null values using the pandas DataFrame function. The code here created a variable named “df” and the outcome of invoking the pd.DataFrame() function is then assigned to this created variable. Inside the parentheses of the pd.DataFrame() function, we have utilized the curly braces and write the names of the columns we want to have in the DataFrame. We have created four columns: Name, Chemistry, English, and Science. Then, we assigned all the columns with different values. We must keep all the columns of the same size. The print function is invoked to print the dataframe.

The output shows the following dataframe:

Now, for each column in our dataframe, we will calculate the amount of non-null records. The count() function for a dataframe is applied in this manner in the most straightforward approach.

In this case, we are applying count() here on the overall “df” dataframe. To accomplish this, we entered the Dataframe’s name, “df”, followed by the .count() function.

When we execute the previous code, it will yield us the outcome shown in the following image:

You can get the total amount of non-missing entries for each column in the result.

Our dataframe comprises a total of six rows. You can notice that the variable “Name” has six values in this instance. There are no empty spaces in this variable. However, specific values contain less than six. For example, science has four non-missing entries, whereas chemistry has five. For this instance, it applies its default settings to the parameter.

Having this knowledge might be helpful when cleaning up the data. Developing a machine learning algorithm could also be advantageous because specific model categories won’t accept missing data.

Example 2: Count the Number of Records in All Rows of a DataFrame Using the pandas count() Method

Now, let us determine how many non-missing entries there are in the rows of the specified dataframe.

The count() method is generally employed to enumerate the columns’ non-missing entries. However, there can be situations where you should look at the rows instead. We’ll utilize the axis property to accomplish this.

Following dataframe construction, the df.count() method calculates the number of values in each row while ignoring any null or NaN entries. Rows are represented by axis=1. Hence, we instruct the code to tally just the entries in the DataFrame’s rows.

As a result, this program considers the count() method, outputs the dataframe row as displayed in the screenshot below, and then loops back to the pandas function.

We have reviewed the data, so we know four columns are in our dataframe. So, a fully populated row should have four non-missing values. However, you may observe that some rows have three or two non-missing data. There are four entries in the first, second, and last row. This indicates that there is missing data in some of the rows. That might be okay, but maybe not, depending on your actions.

Setting axis = “columns” instead will achieve the same result. Because the Axis = 1 and axis = “columns” are equivalent, the amount of non-missing data for the rows is provided when you choose axis = “columns.”

This will yield the same outcome as the one previously shown.

However, we highly advise against using this alternative syntax and instead use axis = 1 because it is quite challenging to grasp and barely makes sense if you are familiar with axes.


In this article, we have learned how to count values in a pandas dataframe. The pandas dataframe.count() method aids in our analysis of the numbers in the Python dataframe. We first created a dataframe using the pandas dataframe function and then applied the DataFrames count method to it. Afterward, we explained to you counting the data in columns and rows. We hope this article will increase your knowledge.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.