Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Count number of columns of a Pandas DataFrame
Next article icon

Count the number of rows and columns of Pandas dataframe

Last Updated : 01 Aug, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we’ll see how we can get the count of the total number of rows and columns in a Pandas DataFrame. There are different methods by which we can do this. Let’s see all these methods with the help of examples.

Example 1: We can use the dataframe.shape to get the count of rows and columns. dataframe.shape[0] and dataframe.shape[1] gives count of rows and columns respectively.




# importing the module
import pandas as pd
  
# creating a DataFrame
dict = {'Name' : ['Martha', 'Tim', 'Rob', 'Georgia'],
        'Marks' : [87, 91, 97, 95]}
df = pd.DataFrame(dict)
  
# displaying the DataFrame
display(df)
  
# fetching the number of rows and columns
rows = df.shape[0]
cols = df.shape[1]
  
# displaying the number of rows and columns
print("Rows: " + str(rows))
print("Columns: " + str(cols))
 
 

Output :

Example 2 : We can use the len() method to get the count of rows and columns. dataframe.axes[0] represents rows and dataframe.axes[1] represents columns. So, dataframe.axes[0] and dataframe.axes[1] gives the count of rows and columns respectively.




# importing the module
import pandas as pd
  
# creating a DataFrame
dict = {'Name':['Martha', 'Tim', 'Rob', 'Georgia'],
        'Marks':[87, 91, 97, 95]}
df = pd.DataFrame(dict)
  
# displaying the DataFrame
display(df)
  
# fetching the number of rows and columns
rows = len(df.axes[0])
cols = len(df.axes[1])
  
# displaying the number of rows and columns
print("Rows: " + str(rows))
print("Columns: " + str(cols))
 
 

Output :

Example 3 : Similar to the example 2, dataframe.index represents rows and dataframe.columns represents columns. So, len(dataframe.index) and len(dataframe.columns) gives count of rows and columns respectively.




# importing the module
import pandas as pd
  
# creating a DataFrame
dict = {'Name':['Martha', 'Tim', 'Rob', 'Georgia'],
        'Marks':[87, 91, 97, 95]}
df = pd.DataFrame(dict)
  
# displaying the DataFrame
display(df)
  
# fetching the number of rows and columns
rows = len(df.index)
cols = len(df.columns)
  
# displaying the number of rows and columns
print("Rows: " + str(rows))
print("Columns: " + str(cols))
 
 

Output :



Next Article
Count number of columns of a Pandas DataFrame

P

parasmadan15
Improve
Article Tags :
  • Python
Practice Tags :
  • python

Similar Reads

  • Count number of rows and columns in Pandas dataframe
    In Pandas understanding number of rows and columns in a DataFrame is important for knowing structure of our dataset. Whether we're cleaning the data, performing calculations or visualizing results finding shape of the DataFrame is one of the initial steps. In this article, we'll explore various ways
    3 min read
  • Count number of columns of a Pandas DataFrame
    Let's discuss how to count the number of columns of a Pandas DataFrame. Lets first make a dataframe. Example: [GFGTABS] Python3 # Import Required Libraries import pandas as pd import numpy as np # Create a dictionary for the dataframe dict = {'Name': ['Sukritin', 'Sumit Tyagi
    2 min read
  • Get the number of rows and number of columns in Pandas Dataframe
    Pandas provide data analysts a variety of pre-defined functions to Get the number of rows and columns in a data frame. In this article, we will learn about the syntax and implementation of few such functions. Method 1: Using df.axes() Method axes() method in pandas allows to get the number of rows a
    3 min read
  • Get number of rows and columns of PySpark dataframe
    In this article, we will discuss how to get the number of rows and the number of columns of a PySpark dataframe. For finding the number of rows and number of columns we will use count() and columns() with len() function respectively. df.count(): This function is used to extract number of rows from t
    6 min read
  • How to Count Distinct Values of a Pandas Dataframe Column?
    Let's discuss how to count distinct values of a Pandas DataFrame column. Using pandas.unique()You can use pd.unique()to get all unique values in a column. To count them, apply len()to the result. This method is useful when you want distinct values and their count. [GFGTABS] Python import pandas as p
    4 min read
  • Count Frequency of Columns in Pandas DataFrame
    When working with data in Pandas counting how often each value appears in a column is one of the first steps to explore our dataset. This helps you understand distribution of data and identify patterns. Now we’ll explore various ways to calculate frequency counts for a column in a Pandas DataFrame.
    2 min read
  • Pandas Append Rows & Columns to Empty DataFrame
    Appending rows and columns to an empty DataFrame in pandas is useful when you want to incrementally add data to a table without predefining its structure. To immediately grasp the concept, here’s a quick example of appending rows and columns to an empty DataFrame using the concat() method, which is
    4 min read
  • Get the datatypes of columns of a Pandas DataFrame
    Let us see how to get the datatypes of columns in a Pandas DataFrame. TO get the datatypes, we will be using the dtype() and the type() function.Example 1 :   [GFGTABS] python # importing the module import pandas as pd # creating a DataFrame dictionary = {'Names':['Simon', 'Josh
    2 min read
  • Pandas DataFrame transpose() Method - Swap Rows and Columns
    Transposing a Pandas DataFrame means switching rows and columns. That means row labels become column headers and column headers become row labels. It is useful when we want to change orientation of our data for better readability and analysis. In this article, we will see some examples to understand
    2 min read
  • Count the NaN values in one or more columns in Pandas DataFrame
    Let us see how to count the total number of NaN values in one or more columns in a Pandas DataFrame. In order to count the NaN values in the DataFrame, we are required to assign a dictionary to the DataFrame and that dictionary should contain numpy.nan values which is a NaN(null) value. Consider the
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences