Count the number of rows and columns of a Pandas dataframe
You can try different methods to get the number of rows and columns of the dataframe:
- len(df)
- len(df.index)
- df.shape[0]
- df[df.columns[0]].count()
- df.count()
- df.size
First let's create a data frame with values.
import pandas as pd
import numpy as np
df = pd.DataFrame()
df['Name'] = ['John', 'Doe', 'Bill','Jim','Harry','Ben']
df['TotalMarks'] = [82, 38, 63,22,55,40]
df['Grade'] = ['A', 'E', 'B','E','C','D']
df['Promoted'] = [True, False,True,False,True,True]
df
Name TotalMarks Grade Promoted
0 John 82 A True
1 Doe 38 E False
2 Bill 63 B True
3 Jim 22 E False
4 Harry 55 C True
5 Ben 40 D True
Pandas DataFrame row count
Using len()
You can use len() method to find the number of rows in dataFrame.
row_count = len(df) # Return number of rows
row_count
6
Or
row_count = len(df.index) # Return number of rows
row_count
6
Using shape[0]
row_count = df.shape[0]
row_count
6
Another method:
row_count = df[df.columns[0]].count()
row_count
6
Get Pandas DataFrame column count
Using shape[1]
col_count = df.shape[1]
col_count
4
Using len()
col_count = len(df.columns)
col_count
4
Pandas DataFrame column with row count
count = df.count()
count
Name 6
TotalMarks 6
Grade 6
Promoted 6
dtype: int64
Total number of rows and column in DataFrame
df.size
24
Related Topics
- Creating an empty Pandas DataFrame
- How to Check if a Pandas DataFrame is Empty
- How to check if a column exists in Pandas Dataframe
- How to delete column from pandas DataFrame
- How to select multiple columns from Pandas DataFrame
- Selecting multiple columns in a Pandas dataframe based on condition
- Selecting rows in pandas DataFrame based on conditions
- How to Drop rows in DataFrame by conditions on column values
- Rename column in Pandas DataFrame
- Get a List of all Column Names in Pandas DataFrame
- How to add new columns to Pandas dataframe?
- Change the order of columns in Pandas dataframe
- Concatenate two columns into a single column in pandas dataframe
- Use a list of values to select rows from a pandas dataframe
- How to iterate over rows in a DataFrame in Pandas
- How to drop rows/columns of Pandas DataFrame whose value is NaN
- How to Export Pandas DataFrame to a CSV File
- Convert list of dictionaries to a pandas DataFrame
- How to set a particular cell value in pandas DataFrame