Count the number of rows and columns of a Pandas dataframe

Count the number of rows and columns of a Pandas dataframe

To count the number of rows and columns of a Pandas DataFrame, you can use the shape attribute. The shape attribute returns a tuple representing the dimensionality of the DataFrame where the first element is the number of rows and the second element is the number of columns.

Here's an example:

import pandas as pd # Sample DataFrame data = { 'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9] } df = pd.DataFrame(data) # Get the number of rows and columns num_rows, num_cols = df.shape print(f'Number of Rows: {num_rows}') print(f'Number of Columns: {num_cols}') 

Output:

Number of Rows: 3 Number of Columns: 3 

Additionally, if you only want to get the number of rows or columns individually:

  • Number of Rows:

    num_rows = len(df) 

    or

    num_rows = df.shape[0] 
  • Number of Columns:

    num_cols = len(df.columns) 

    or

    num_cols = df.shape[1] 

More Tags

spaces uiactivityindicatorview android bitbucket-server cpu-usage orchardcms youtube html5-canvas xcodebuild google-apps-script

More Programming Guides

Other Guides

More Programming Examples