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
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. See also · 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 ...
🌐
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 - ... import pandas as pd data = pd.read_csv("/content/nba.csv") size = data.size print("Size = {}".format(size)) ... The df.shape function returns a tuple representing dimensions of the DataFrame.
🌐
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.
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)
🌐
Pandas
pandas.pydata.org › docs › dev › reference › api › pandas.DataFrame.shape.html
pandas.DataFrame.shape — pandas documentation
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. See also · 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
🌐
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. ... Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more!
Find elsewhere
🌐
Medium
medium.com › @heyamit10 › understanding-pandas-shape-ba74dadf8387
Understanding pandas.shape
March 6, 2025 - Simple Answer: It tells you the number of rows and columns in your DataFrame or Series. Important Note: It’s an attribute, not a method. So, you don’t need parentheses when using it.
🌐
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], ...
🌐
DataCamp
datacamp.com › tutorial › python-dataframe-size
Finding the Size of a DataFrame in Python | DataCamp
February 14, 2024 - Python pandas is a library that allows analysts to easily work with DataFrames. This library has a straightforward shape method used to find the size of a DataFrame. import pandas as pd # Creating a sample DataFrame df = pd.DataFrame({'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 22], 'City': ['New York', 'San Francisco', 'Los Angeles']}) # Using shape to get the size rows, columns = df.shape print(f"Number of rows: {rows}, Number of columns: {columns}")
🌐
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 ...
🌐
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) Yields below output. ... A column in DataFrame is represented as a Series, so getting the shape of the DataFrame is same as getting the shape of the Series.
🌐
Polars
docs.pola.rs › py-polars › html › reference › dataframe › api › polars.DataFrame.shape.html
polars.DataFrame.shape — Polars documentation
property DataFrame.shape: tuple[int, int][source]# Get the shape of the DataFrame. Examples · >>> df = pl.DataFrame({"foo": [1, 2, 3, 4, 5]}) >>> df.shape (5, 1) On this page
🌐
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 ·
🌐
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.
🌐
TutorialsPoint
tutorialspoint.com › python-pandas-how-to-use-pandas-dataframe-property-shape
Python Pandas – How to use Pandas DataFrame Property: shape
February 29, 2024 - 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'])
🌐
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 - To use the shape function in Python, first import the Pandas library with import pandas as pd and create a DataFrame using df = pd.DataFrame(data). Then, obtain the dimensions as a tuple using dimensions = df.shape.
🌐
Reddit
reddit.com › r/learnpython › my dataframe shape is not transforming correctly
r/learnpython on Reddit: My DataFrame shape is not transforming correctly
June 19, 2024 -

Hi all

I'm doing a course on Udemy so this code probably looks familiar to some of you. I'm trying to get a list of prices from the original dataset (shape is (506,), and convert it to log prices.

Converting it to log prices results in the same (506,0) shape, but when I try to convert this into a DataFrame, the shape ends up being (0,1) so I have issues later down the line trying to merge this with another data frame that's shaped (506,11).

Someone in course comments mentioned that using np.log1p instead of np.log will fix the issue, but unfortunately I'm having the same issue.

Any help would be greatly appreciated. I've tried asking in the course comments, and even tried google and troubleshooting with ChatGPT but no dice 🥲

Code:

# Gather Data
data = pd.DataFrame(data=boston_dataset.data, columns=boston_dataset.feature_names)
data.head()
features = data.drop(['INDUS', 'AGE'], axis=1)

# Check the shape of the target values in the dataset
print("Shape of boston_dataset.target:", boston_dataset.target.shape)

# Convert to log prices
#log_prices = np.log(boston_dataset.target)
log_prices = np.log1p(boston_dataset.target)

# Ensure log_prices has the correct shape
print("Shape of log_prices:", log_prices.shape)
print("First 5 elements of log_prices:", log_prices[:5])

# Create a new DataFrame for the target prices
target = pd.DataFrame(log_prices, columns=['PRICE'])

# Check the shape and contents of the target DataFrame
print("Target DataFrame shape:", target.shape)
print("First 5 rows of target DataFrame:\n", target.head())

# Check if the DataFrames are empty
if features.empty:
    print("Error: Features DataFrame is empty")
if target.empty:
    print("Error: Target DataFrame is empty")

# If 'CHAS' is categorical, convert it to numeric
if features['CHAS'].dtype == 'category':
    features['CHAS'] = features['CHAS'].astype('int')

# Convert RAD to integer
if features['RAD'].dtype == 'category':
    features['RAD'] = features['RAD'].astype('int')

The outputs for the above print statements are:

Shape of boston_dataset.target: (506,)
Shape of log_prices: (506,)
First 5 elements of log_prices: 0    3.218876
1    3.117950
2    3.575151
3    3.538057
4    3.616309
Name: MEDV, dtype: float64
Target DataFrame shape: (0, 1)
First 5 rows of target DataFrame:
 Empty DataFrame
Columns: [PRICE]
Index: []
Error: Target DataFrame is empty
🌐
Reddit
reddit.com › r/learnpython › what does .isnull() and shape[0] do in pandas?
r/learnpython on Reddit: What does .isnull() and shape[0] do in Pandas?
October 1, 2020 -

Hey

I am pretty new to Pandas. Do you know what the first and second line do?

Specifically, I try to understand what .isnull(),:] is doing

and df.shape[0] and the 2 at the end ? :S

Thanks for your help.

salary_nan_df = survey_df.loc[survey_df['ConvertedSalary'].isnull(), :]

percentage = round((salary_nan_df.shape[0] / survey_df.shape[0]) * 100, 2)

print(str(percentage)+"% ("+str(salary_nan_df.shape[0])+") of responders have not filled in their salary.")

51.75% (51153) of responders have not filled in their salary.

🌐
Medium
medium.com › @i-jesse › exploring-data-frames-f10218a5d3bd
Exploring Data Frames. shape, head, tail, info, describe, | by I. Jesse | Medium
March 23, 2025 - These operations are carried out on a data frame after it has been imported and read. On how to do that, you can check here or below. medium.com · Calling the shape returns a vector that shows the number of rows and columns. df.shape ·