How to Get the Number of Rows and Columns in Dataframe Python?

In this tutorial, we'll look at how to determine how many rows and columns are in a DataFrame. There are several ways we may accomplish this. Let's examine each of these approaches using examples.

Quick Methods for Counting Rows in a Pandas DataFrame

See the samples below if you need to know how many rows there are in a given Pandas DataFrame quickly.

Code

Dataframe.shape()

We can use dataframe.shape to calculate the number of dataframe rows and columns. The row and column of the dataframe counts are provided by dataframe.shape[0] and dataframe.shape[1], respectively.

Code

Output

  row_1  row_2
0     A      6
1     B      7
2     C      8
3     D      9
Rows:  4
Columns:  2

Dataframe.len()

The len() function can determine the number of the Dataframe's rows and columns. Rows and columns are represented by dataframe.axes[0] and dataframe.axes[1]. As a result, dataframe.axes[0] and dataframe.axes[1] provide the number of rows and columns, respectively.

Code

Output

Rows:  4
Columns:  2

Dataframe.index() and Dataframe.columns()

Dataframe.index provides rows, and dataframe.columns provides columns, much like in Example 2. Len() functions, len(dataframe.index), and len(dataframe.columns) correspondingly return the number of the Dataframe's rows and columns. RangeIndex(start = 0, stop = 7, step = 1) is returned by df.index and can be used with len() to obtain the count. Len(df) is another option, although it runs less quickly than len(df.index) since it requires one fewer function call. These return the count more quickly than df.shape[0].

Code

Output

Rows:  4
Columns:  2

Dataframe.count()

Given its performance, this strategy is not advised; yet, we must still discuss it because it is one of the ways to determine the number of rows in a DataFrame. When determining the count, we should note that this excludes data from columns containing None or Nan.

Code

Output

Rows:  4
Columns:  2

You have studied using DataFrame to find or obtain the count of rows (merely row count) in a DataFrame from this tutorial. You learned how to obtain the form of the DataFrame from len(DataFrame.index), len(DataFrame.axes[0]), DataFrame.count(), and others.






Latest Courses