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.

Answer from hpaulj on Stack Overflow
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.shape.html
pandas.DataFrame.shape — pandas 3.0.2 documentation
Return a tuple representing the dimensionality of the DataFrame · Unlike the len() method, which only returns the number of rows, shape provides both row and column counts, making it a more informative method for understanding dataset size
🌐
W3Schools
w3schools.com › python › pandas › ref_df_shape.asp
Pandas DataFrame shape Property
Return the shape of the DataFrame: ... returns a tuple containing the shape of the DataFrame. The shape is the number of rows and columns of the DataFrame ·...
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)
🌐
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.
🌐
Medium
medium.com › @heyamit10 › understanding-pandas-shape-ba74dadf8387
Understanding pandas.shape
March 6, 2025 - Let’s get straight to the point — pandas.shape is your go-to tool when you want to know the size of your DataFrame or Series. Think of it as asking your dataset, "Hey, how big are you?" and it responds with a neat little tuple like (rows, ...
🌐
pandas
pandas.pydata.org › pandas-docs › dev › reference › api › pandas.DataFrame.shape.html
pandas.DataFrame.shape — pandas documentation
Return a tuple representing the dimensionality of the DataFrame · Unlike the len() method, which only returns the number of rows, shape provides both row and column counts, making it a more informative method for understanding dataset size
🌐
DataCamp
datacamp.com › tutorial › python-dataframe-size
Finding the Size of a DataFrame in Python | DataCamp
February 14, 2024 - The df.shape method provides information about the number of rows and columns in a DataFrame quickly and easily.
Find elsewhere
🌐
Codecademy
codecademy.com › docs › python:pandas › dataframe › .shape
Python:Pandas | DataFrame | .shape | Codecademy
December 30, 2022 - The .shape property returns a tuple of information about the dimensions (rows and columns) of a DataFrame object.
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › pandas get dataframe shape
Pandas Get DataFrame Shape - Spark By {Examples}
March 27, 2024 - We can get the shape of Pandas DataFrame using the shape attribute. The shape is nothing but a number of rows and columns of the
🌐
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 - The shape attribute of a DataFrame returns a tuple in the form (number of rows, number of 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], ...
🌐
Python Examples
pythonexamples.org › pandas-dataframe-shape
Pandas DataFrame.shape
Learn how to use pandas DataFrame.shape to retrieve the dimensions of a DataFrame. Includes examples for dynamic operations, empty DataFrames, and filtering.
🌐
w3resource
w3resource.com › pandas › dataframe › dataframe-shape.php
Pandas DataFrame property: shape - w3resource
Pandas DataFrame - shape property: The shape property is used to return a tuple representing the dimensionality of the DataFrame.
🌐
Spark Code Hub
sparkcodehub.com › pandas › basics › data-dimensions-shape
Understanding Data Dimensions and Shape in Pandas: A Comprehensive Guide
shape after loading data to validate its structure: ... For data loading, see read-write-csv. ... This indicates the Series has 5 elements. For Series creation, see series. ... This confirms DataFrames are two-dimensional (rows and columns) and Series are one-dimensional (elements). ... For index details, see series-index. ... df = pd.read_excel('data.xlsx') if df.shape[0] > 0 and df.shape[1] == 3: print("Data loaded successfully with expected structure") else: print("Unexpected data structure")
🌐
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
🌐
DataCamp
datacamp.com › tutorial › pandas-tutorial-dataframe-python
Pandas Tutorial: DataFrames in Python | DataCamp
December 12, 2022 - df = pd.DataFrame(np.array([[1, ... different information on your DataFrame: the shape property will provide you with the dimensions of your DataFrame....
🌐
AskPython
askpython.com › home › python shape function: find dimensions of arrays and dataframes
Python Shape Function: Find Dimensions of Arrays and DataFrames - AskPython
April 8, 2023 - The shape function in Python yields a tuple that signifies the dimensions of a NumPy array or a Pandas DataFrame. In the case of a DataFrame, the tuple indicates the quantity of rows and columns.
🌐
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…