pd.shape will give you the number of rows and columns present in the dataFrame.

where, df.shape[0] will give you the total rows present in the dataFrame.

and, df.shape[1] will give you the number of columns present in the dataFrame.

Example:

df = pd.DataFrame({'Date':['10/2/2011', '11/2/2011', '12/2/2011'], 
                    'Phrases':['I have a cool family', 'I like avocados', 'I would like to go to school']})


df
Out[26]: 
        Date                       Phrases
0  10/2/2011          I have a cool family
1  11/2/2011               I like avocados
2  12/2/2011  I would like to go to school

df.shape
Out[27]: (3, 2)

df.shape[0]   #number of rows
Out[28]: 3

df.shape[1]   #number of columns
Out[29]: 2
Answer from Suhas Mucherla on Stack Overflow
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.shape.html
pandas.DataFrame.shape — pandas 3.0.1 documentation
numpy.ndarray.shape · Tuple of array dimensions. Examples · >>> df = pd.DataFrame({"col1": [1, 2], "col2": [3, 4]}) >>> df.shape (2, 2) >>> df = pd.DataFrame({"col1": [1, 2], "col2": [3, 4], "col3": [5, 6]}) >>> df.shape (2, 3) On this page
🌐
W3Schools
w3schools.com › python › pandas › ref_df_shape.asp
Pandas DataFrame shape Property
import pandas as pd df = pd.read_csv('data.csv') print(df.shape) Try it Yourself » · The shape property returns a tuple containing the shape of the DataFrame. The shape is the number of rows and columns of the DataFrame · dataframe.shape · a Python Tuple showing the number of rows and columns. ❮ DataFrame Reference · ★ +1 ·
🌐
Python Examples
pythonexamples.org › pandas-dataframe-shape
Pandas DataFrame.shape
You can use the shape property to get the number of rows and columns in a DataFrame. import pandas as pd # Create a DataFrame data = { 'Name': ['Arjun', 'Ram', 'Priya'], 'Age': [25, 30, 35], 'Salary': [70000.5, 80000.0, 90000.0] } df = pd.DataFrame(data) # Get the shape of the DataFrame print("Shape of the DataFrame:") print(df.shape) Shape of the DataFrame: (3, 3) You can use the shape property to dynamically access rows and columns.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-pandas-df-size-df-shape-and-df-ndim
Pandas df.size, df.shape and df.ndim Methods - GeeksforGeeks
July 11, 2025 - A DataFrame is always two-dimensional (rows and columns) so it returns 2. A Series is one-dimensional so it returns 1. Syntax: dataframe.ndim · Return: 1 for a Series (one-dimensional).
Top answer
1 of 2
3

Get into an interactive Python session with numpy and pandas, and experiment

Make a dataframe:

In [394]: df=pd.DataFrame(np.eye(3))                                            
In [395]: df                                                                    
Out[395]: 
     0    1    2
0  1.0  0.0  0.0
1  0.0  1.0  0.0
2  0.0  0.0  1.0

Check its shape. That's a tuple (basic Python object):

In [396]: df.shape                                                              
Out[396]: (3, 3)
In [397]: df.shape[0]     # first element of the tuple                                                          
Out[397]: 3

Repeat with the shape parameter is just like using the number 3:

In [398]: np.repeat('red', df.shape[0])                                         
Out[398]: array(['red', 'red', 'red'], dtype='<U3')

Pandas and numpy are running in Python. So the regular evaluation order of Python applies.

2 of 2
0

This part (red_df.shape[0]) just to return an integer with the total number of rows in the red_df to create the new add column 'Color' with the same number of raws of its related red_df so, when we append it later with the white_df, it doesn't shift down the other white_df and creatw empty rows on the other columns.

You can simply delete this section and write it like this:

color_red = np.repeat('red', red_df.shape[0])
color_red = np.repeat('red', 1599)

Full program will be

import pandas as pd
import numpy as np

df_red = pd.read_csv('winequality-red.csv',sep=';')

df_white = pd.read_csv('winequality-white.csv',sep=';')

print(df_red.info())

print(df_red.shape[0])

# shape[0} refer to the number of columns which is 1599 shape[1] refer to the number of rows which is 12

# create color array for red dataframe
color_red = np.repeat('red', 1599)

# create color array for white dataframe
color_white = np.repeat('white', df_white.shape[0])


df_red['color'] = color_red

df_white['color'] = color_white

#combine data frame into one data frame called wine_df

wine_df = df_red.append(df_white)

print(wine_df.head())

wine_df.to_csv('winequality_edited.csv', index=False)
🌐
DeepLearning.AI
community.deeplearning.ai › course q&a › machine learning specialization › advanced learning algorithms
Difference between .shape[0] and .shape[1] - Advanced Learning Algorithms - DeepLearning.AI
August 27, 2022 - Hi, In the course, i find sometimes the code is written as m=X.shape[0] and n=w.shape[1]. Can you tell me the difference between these 2 functions, .shape[0] and .shape[1], though both returns the number of columns in a…
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › pandas get dataframe shape
Pandas Get DataFrame Shape - Spark By {Examples}
March 27, 2024 - # Get the shape of Pandas dataframe print(" Shape of DataFrame:", df.shape) # Output: # Shape of DataFrame: (35, 5)
Find elsewhere
🌐
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.DataFrame.shape.html
pandas.DataFrame.shape — pandas 3.0.2 documentation
numpy.ndarray.shape · Tuple of array dimensions. Examples · >>> df = pd.DataFrame({"col1": [1, 2], "col2": [3, 4]}) >>> df.shape (2, 2) >>> df = pd.DataFrame({"col1": [1, 2], "col2": [3, 4], "col3": [5, 6]}) >>> df.shape (2, 3) On this page
🌐
pandas
pandas.pydata.org › pandas-docs › dev › reference › api › pandas.DataFrame.shape.html
pandas.DataFrame.shape — pandas documentation
numpy.ndarray.shape · Tuple of array dimensions. Examples · >>> df = pd.DataFrame({"col1": [1, 2], "col2": [3, 4]}) >>> df.shape (2, 2) >>> df = pd.DataFrame({"col1": [1, 2], "col2": [3, 4], "col3": [5, 6]}) >>> df.shape (2, 3) On this page
🌐
Educative
educative.io › answers › how-to-return-the-shape-of-a-dataframe-in-pandas
How to return the shape of a DataFrame in Pandas
The DataFrame.shape returns a tuple of array dimensions representing the shape of a given DataFrame. ... Line 4: We import the pandas library. Line 7: We create a DataFrame and name it df using the pandas.DataFrame() function.
🌐
TutorialsPoint
tutorialspoint.com › python-pandas-how-to-use-pandas-dataframe-property-shape
Python Pandas – How to use Pandas DataFrame Property: shape
February 29, 2024 - Rows: 100 Columns: 8 id product engine avgmileage price height_mm width_mm productionYear 1 2 Car Diesel 21 16500 1530 1735 2020 4 5 Car Gas 18 17450 1530 1780 2018 5 6 Car Gas 19 15250 1530 1790 2019 8 9 Car Diesel 23 16925 1530 1800 2018 ... import pandas as pd df = pd.read_csv('products.csv ') print("Rows:",df.shape[0],"Columns:",df.shape[1]) df1 = df.head(10) print(df1[df1['product']=='Car'])
🌐
Medium
medium.com › @heyamit10 › understanding-pandas-shape-ba74dadf8387
Understanding pandas.shape
March 6, 2025 - This line: df.shape tells you how many rows and columns your dataset has. Example output might be something like (1000000, 15), meaning 1 million rows and 15 columns.
🌐
Apache
spark.apache.org › docs › latest › api › python › reference › pyspark.pandas › api › pyspark.pandas.DataFrame.shape.html
pyspark.pandas.DataFrame.shape — PySpark 4.1.1 documentation
property DataFrame.shape# Return a tuple representing the dimensionality of the DataFrame. Examples · >>> df = ps.DataFrame({'col1': [1, 2], 'col2': [3, 4]}) >>> df.shape (2, 2) >>> df = ps.DataFrame({'col1': [1, 2], 'col2': [3, 4], ...
🌐
Polars
docs.pola.rs › py-polars › html › reference › dataframe › api › polars.DataFrame.shape.html
polars.DataFrame.shape — Polars documentation
Get the shape of the DataFrame. Examples · >>> df = pl.DataFrame({"foo": [1, 2, 3, 4, 5]}) >>> df.shape (5, 1) On this page
🌐
Codecademy
codecademy.com › docs › python:pandas › dataframe › .shape
Python:Pandas | DataFrame | .shape | Codecademy
December 30, 2022 - The following example initiates a DataFrame and uses .shape to return the number of rows and columns: import pandas as pd · d = {"col1" : [1, 2 ,3], "col2" : [4, 5, 6]} df = pd.DataFrame(data = d) print(df.shape) Copy to clipboard · Copy to clipboard · This will print the following: (3, 2) Copy to clipboard ·
🌐
Note.nkmk.me
note.nkmk.me › home › python › pandas
pandas: Get the number of rows, columns, elements (size) in DataFrame | note.nkmk.me
May 4, 2025 - print(df.shape) # (891, 12) print(df.shape[0]) # 891 print(df.shape[1]) # 12 · source: pandas_len_shape_size.py · You can unpack this tuple to assign the row and column counts to individual variables: Unpack a tuple and list in Python · row, col = df.shape print(row) # 891 print(col) # 12 ·
🌐
Finxter
blog.finxter.com › understanding-the-shape-property-in-pythons-pandas-dataframe
Understanding the ‘shape’ Property in Python’s Pandas DataFrame – Be on the Right Side of Change
March 7, 2024 - Using the shape property is the most straightforward approach to get the size of a DataFrame. The property returns a tuple where the first element is the number of rows (i.e., the length of the DataFrame) and the second is the number of columns. ... import pandas as pd # Creating a sample DataFrame df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}) # Getting the shape of the DataFrame shape = df.shape print("DataFrame Shape:", shape)
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 0.17.0 › generated › pandas.DataFrame.shape.html
pandas.DataFrame.shape — pandas 0.17.0 documentation
DataFrame.shape¶ · Return a tuple representing the dimensionality of the DataFrame. index · modules | next | previous | pandas 0.17.0 documentation » · API Reference » · © Copyright 2008-2014, the pandas development team. Created using Sphinx 1.2.3.