Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Pandas – Count the rows and columns in a DataFrame
To count the rows and columns in a DataFrame, use the shape property. This returns a tuple where the first value is the number of rows and the second value is the number of columns.
What is the shape Property?
The shape property returns the dimensions of a DataFrame as a tuple (rows, columns). It's a quick way to understand the size of your dataset ?
import pandas as pd
# Create a sample DataFrame
data = {'Car': ['Audi', 'Porsche', 'BMW', 'Mercedes'],
'Place': ['Bangalore', 'Mumbai', 'Delhi', 'Hyderabad'],
'UnitsSold': [80, 110, 95, 80]}
df = pd.DataFrame(data)
print("DataFrame:")
print(df)
print("\nShape:", df.shape)
DataFrame:
Car Place UnitsSold
0 Audi Bangalore 80
1 Porsche Mumbai 110
2 BMW Delhi 95
3 Mercedes Hyderabad 80
Shape: (4, 3)
Getting Individual Counts
You can also get the row and column counts separately using indexing ?
import pandas as pd
data = {'Car': ['Audi', 'Porsche', 'BMW', 'Mercedes', 'Lamborghini'],
'Place': ['Bangalore', 'Mumbai', 'Delhi', 'Hyderabad', 'Pune'],
'UnitsSold': [80, 110, 95, 80, 100]}
df = pd.DataFrame(data)
# Get total dimensions
rows, columns = df.shape
print(f"Total rows: {rows}")
print(f"Total columns: {columns}")
# Alternative methods
print(f"Rows using len(): {len(df)}")
print(f"Columns using len(): {len(df.columns)}")
Total rows: 5 Total columns: 3 Rows using len(): 5 Columns using len(): 3
Practical Example with Data Analysis
Here's a complete example showing how to use shape for data analysis ?
import pandas as pd
# Create a larger dataset
car_data = {
'Car': ['Audi', 'Porsche', 'RollsRoyce', 'BMW', 'Mercedes',
'Lamborghini', 'Audi', 'Mercedes', 'Lamborghini'],
'Place': ['Bangalore', 'Mumbai', 'Pune', 'Delhi', 'Hyderabad',
'Chandigarh', 'Mumbai', 'Pune', 'Delhi'],
'UnitsSold': [80, 110, 100, 95, 80, 80, 100, 120, 100]
}
df = pd.DataFrame(car_data)
print("Complete DataFrame:")
print(df)
print(f"\nDataset dimensions: {df.shape}")
print(f"Total data points: {df.shape[0] * df.shape[1]}")
# Show first 5 rows
print(f"\nFirst 5 rows:")
print(df.head(5))
Complete DataFrame:
Car Place UnitsSold
0 Audi Bangalore 80
1 Porsche Mumbai 110
2 RollsRoyce Pune 100
3 BMW Delhi 95
4 Mercedes Hyderabad 80
5 Lamborghini Chandigarh 80
6 Audi Mumbai 100
7 Mercedes Pune 120
8 Lamborghini Delhi 100
Dataset dimensions: (9, 3)
Total data points: 27
First 5 rows:
Car Place UnitsSold
0 Audi Bangalore 80
1 Porsche Mumbai 110
2 RollsRoyce Pune 100
3 BMW Delhi 95
4 Mercedes Hyderabad 80
Alternative Methods
Besides shape, you can use other methods to get DataFrame dimensions ?
import pandas as pd
data = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}
df = pd.DataFrame(data)
print("Using different methods:")
print(f"shape: {df.shape}")
print(f"len(df): {len(df)}")
print(f"df.index.size: {df.index.size}")
print(f"len(df.columns): {len(df.columns)}")
print(f"df.size (total elements): {df.size}")
Using different methods: shape: (3, 3) len(df): 3 df.index.size: 3 len(df.columns): 3 df.size (total elements): 9
Conclusion
The shape property is the most efficient way to get DataFrame dimensions, returning (rows, columns). Use len(df) for just row count and len(df.columns) for column count when needed separately.
