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
  • Data preprocessing
  • Data Manipulation
  • Data Analysis using Pandas
  • EDA
  • Pandas Exercise
  • Pandas AI
  • Numpy
  • Matplotlib
  • Plotly
  • Data Analysis
  • Machine Learning
  • Data science
Open In App
Next Article:
Count the number of rows and columns of Pandas dataframe
Next article icon

Get the number of rows and number of columns in Pandas Dataframe

Last Updated : 02 Jul, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report

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 and columns in a go. It accepts the argument ‘0’ for rows and ‘1’ for columns.

Syntax: df.axes[0 or 1]

Parameters:
0: for number of Rows
1: for number of columns

Example:




# import pandas library
import pandas as pd
    
# dictionary with list object in values
details = {
    'Name' : ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi'],
    'Age' : [23, 21, 22, 21],
    'University' : ['BHU', 'JNU', 'DU', 'BHU'],
}
    
# creating a Dataframe object 
df = pd.DataFrame(details, columns = ['Name', 'Age', 'University'],\
                  index = ['a', 'b', 'c', 'd'])
    
# Get the number of rows and columns
rows = len(df.axes[0])
cols = len(df.axes[1])
  
# Print the number of rows and columns
print("Number of Rows: " + str(rows))
print("Number of Columns: " + str(cols))
 
 

Output:

  Number of Rows: 4  Number of Columns: 3  

Method 2: Using df.info() Method

df.info() method provides all the information about the data frame, including the number of rows and columns.

Syntax:

df.info

Example:




# import pandas library
import pandas as pd
    
# dictionary with list object in values
details = {
    'Name' : ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi'],
    'Age' : [23, 21, 22, 21],
    'University' : ['BHU', 'JNU', 'DU', 'BHU'],
}
    
# creating a Dataframe object 
df = pd.DataFrame(details, columns = ['Name', 'Age', 'University'],
                  index = ['a', 'b', 'c', 'd'])
    
# Get the info of data frame
df.info()
 
 

Output:

python-pandas-rows-and-number-of-columns

Here in the above code, the value in the Index gives the number of rows and the value in Data columns gives the number of columns.

Method 3: Using len() Method

len() method is used to get the number of rows and number of columns individually.

Syntax:

len(df)  and  len(df.columns)

Example 1: Get the number of rows




# import pandas library
import pandas as pd
    
# dictionary with list object in values
details = {
    'Name' : ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi'],
    'Age' : [23, 21, 22, 21],
    'University' : ['BHU', 'JNU', 'DU', 'BHU'],
}
    
# creating a Dataframe object 
df = pd.DataFrame(details, columns = ['Name', 'Age', 'University'],
                  index = ['a', 'b', 'c', 'd'])
    
# Get the number of rows
print("Number of Rows:", len(df))
 
 

Output:

  Number of Rows: 4  

Example 2: Get the number of columns




# import pandas library
import pandas as pd
    
# dictionary with list object in values
details = {
    'Name' : ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi'],
    'Age' : [23, 21, 22, 21],
    'University' : ['BHU', 'JNU', 'DU', 'BHU'],
}
    
# creating a Dataframe object 
df = pd.DataFrame(details, columns = ['Name', 'Age', 'University'],
                  index = ['a', 'b', 'c', 'd'])
    
# Get the number of columns
print("Number of Columns:", len(df.columns))
 
 

Output:

  Number of Columns: 3  

Method 4: Using df.shape() Method

df.shape() method returns the number of rows and columns in the form of a tuple.

Example:




# import pandas library
import pandas as pd
    
# dictionary with list object in values
details = {
    'Name' : ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi'],
    'Age' : [23, 21, 22, 21],
    'University' : ['BHU', 'JNU', 'DU', 'BHU'],
}
    
# creating a Dataframe object 
df = pd.DataFrame(details, columns = ['Name', 'Age', 'University'],
                  index = ['a', 'b', 'c', 'd'])
    
# Get the number of Rows and columns
df.shape
 
 

Output:

(4, 3)


Next Article
Count the number of rows and columns of Pandas dataframe

A

Akanksha_Rai
Improve
Article Tags :
  • Python
  • Python pandas-dataFrame
  • Python-pandas
Practice Tags :
  • python

Similar Reads

  • Count the number of rows and columns of Pandas dataframe
    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 column
    2 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
  • 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 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
  • How to get column and row names in DataFrame?
    While analyzing the real datasets which are often very huge in size, we might need to get the rows or index names and columns names in order to perform certain operations.  Note: For downloading the nba dataset used in the below examples Click Here  Getting row names in Pandas dataframe First, let's
    4 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
  • Change column names and row indexes in Pandas DataFrame
    Given a Pandas DataFrame, let's see how to change its column names and row indexes. About Pandas DataFramePandas DataFrame are rectangular grids which are used to store data. It is easy to visualize and work with data when stored in dataFrame. It consists of rows and columns.Each row is a measuremen
    4 min read
  • Change Data Type for one or more columns in Pandas Dataframe
    When working with data in Pandas working with right data types for your columns is important for accurate analysis and efficient processing. Pandas offers several simple ways to change or convert the data types of columns in a DataFrame. In this article, we'll look at different methods to help you e
    3 min read
  • How to Get Column Names in Pandas Dataframe
    While analyzing the real datasets which are often very huge in size, we might need to get the pandas column names in order to perform certain operations. The simplest way to get column names in Pandas is by using the .columns attribute of a DataFrame. Let's understand with a quick example: [GFGTABS]
    4 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