Data Frame Dimensions

We wish to get the number of rows and / or the number of columns of a data frame.

Shape

We wish to obtain both the number of rows and number of columns of a data frame.

df.shape

Here is how this works:

  • Pandas data frames have a .shape attribute which returns the number or rows and number of columns of the data frame (as a Tuple).
  • Foe example, if the data frame df had 100 rows and 10 columns, df.shape would return (100, 10).

Row Count

We wish to obtain the number of rows of a data frame.

len(df)

Here is how this works:

  • We pass the data frame df to the function len().
  • len() returns the number of rows of the data frame (as an integer).

Column Count

We wish to obtain the number of columns of a data frame.

len(df.columns)

Here is how this works:

We obtain the number of columns of a data frame by using len() to count the number of column names returned by df.columns.

PYTHON
I/O