Python

Count the Number of Rows and Columns in a DataFrame

To perform the proper analysis, we need to count the number of rows and columns because they can help us to know the Frequency or Occurrence of your data.

In this article, we are going to see five different types of ways that can help us to count the total number of rows and columns using the Pandas library.

  1. Using the shape method
  2. Using the len(df.axes) method
  3. Using the dataframe.index (rows) and dataframe.columns
  4. Using the method using df.info( )
  5. Using the method Using df.count()

Method 1: Using the Shape Method

The first method to calculate the rows and columns is the shape method. As we know, the shape method is used to get the table height and width. The shape gives us the result in tuple form with two values. In these two values, the first value of the tuple belongs to the height and the other value (second value) belongs to the width of the table.

So, the same technique can also be used in the dataframe because the dataframe itself is a table which has rows and columns.

  • In cell number [1]: Import the Pandas library as pd.
  • In cell number [2]: We created a dict (dictionary) object and then convert that dict object to a DataFrame using the Pandas library.
  • In cell number [3]: We print the converted dict to DataFrame (df).
  • In cell number [4]: We just print the shape to check what value it stores. We got values that are equal to rows (4) and columns (3).
  • In cell number [5]: So, now we can print the number of rows of the df (DataFrame) using the shape[0] which belongs to the first value of the tuple and columns using the shape[1] which belongs to the second value of the tuple. The same individually we print the result in cell number [6] for rows and columns in the cell number [7].

Method 2: Using the len(df.axes) Method

The next method which we are going to use is the df.axes method. The df.axes method is somewhat similar to the shape method. But the main difference is that the shape method will give direct results of the rows and columns in tuple form. But the df.axes if we print as shown in the cell number [52] below, which stores the index values of the rows and columns.

  • In cell number [50]: We created a dict (dictionary) object and then convert that dict object to a DataFrame using the Pandas library.
  • In cell number [51]: We print the converted dict to DataFrame (df).
  • In cell number [52]: We print the df.axes to see what they store values. We can see the df.axes store the index values of the rows and columns.
  • In cell number [53]: Now, we count the number of rows using the len(df.axes[0]) method as shown above. The value 0 belongs to the row index.
  • In cell number [54]: We calculate the number of columns using the len( df.axes[1]). The value 1 belongs to the column index.

Method 3: Using the dataframe.index (rows) and dataframe.columns

The next method which we are going to use is dataframe.index (rows) and dataframe.columns. This method is also similar to the above method (df.axes) which we already discussed. But to fetch the rows and columns, the way is different, which you will see below.

  • In cell number [55]: We created a dict (dictionary) object and then convert that dict object to a DataFrame using the Pandas library.
  • In cell number [56]: We print the converted dict to DataFrame (df).
  • In cell number [57]: We print the df.index to see what they have values. We found from the result that the df.index has all the index count from start to end of the row.
  • In cell number [58]: We print the df.columns and found that it has all the columns names.
  • In cell number [59]: We then calculate the index (rows) using the len(df.index) method as shown above in cell number [59] and assign the value to a variable row. And similar, we do the count for the columns and assign that value to another variable cols.
  • In cell number [60]: We print the both variables (rows and cols) and get the result 4 and 3 respectively.

Method 4: Using the method using df.info( )

The next method which we are going to discuss to count the rows and columns is df.info ( ). This method is a little bit tricky, which means you will not get the rows and columns as we have seen results in the previous method directly. The reason behind that is that when we run this method, we get the rows and columns values along with other information of the dataframe as you will see in the below result.

  • In cell number [61]: We created a dict (dictionary) object and then convert that dict object to a DataFrame using the Pandas library.
  • In cell number [62]: We print the converted dict to DataFrame (df).
  • In cell number [63]: We print the df.info() and got all information about the dataframe along with the total number of rows and columns. So, the tricks here are we have to filter the result to get the rows and columns of the dataframe.

Method 5:  Using the df.count() Method

The next count method which we are going to discuss is df.count( ). This method can be used to count both rows and columns. To count the total number of rows, we use the df.count ( ) method and for the columns we use the df.count(axis=’columns’).

  • In cell number [64]: We created a dict (dictionary) object and then convert that dict object to a DataFrame using the Pandas library.
  • In cell number [65]: We print the converted dict to DataFrame (df).
  • In cell number [66]: We print the df.count( ) to check the total number of rows and got the result in the form of counts because it will not count the null value. It is little bit tricky to get the proper result, so people not choose this method.
  • In cell number [67]: We count the columns using theas df.count(axis=’columns’).

Conclusion

So, we have seen different kinds of methods to count the rows and columns. In which the best method is the index and shape because they will give the instant result of the total number of the of rows and columns, and we don’t have to perform extra work as we have seen in the other methods like df.count() and df.info().

About the author

Shekhar Pandey