train.Id is a pandas Series and is one dimensional. train is a pandas DataFrame and is two dimensional. shape is an attribute that both DataFrames and Series have. It is always a tuple. For a Series the tuple has only only value (x,). For a DataFrame shape is a tuple with two values (x, y). So train.Id.shape[0] would also return 1467. However, train.Id.shape[1] would produce an error while train.shape[1] would give you the number of columns in train.

Furthermore, pandas Panel objects are three dimensional and shape for it returns a tuple (x, y, z)

train = pd.DataFrame(dict(Id=np.arange(1437), A=np.arange(1437)))

print(train.shape)
print(train.Id.shape)

(1437, 2)
(1437,)
Answer from piRSquared on Stack Overflow
🌐
Medium
medium.com › @heyamit10 › understanding-pandas-shape-ba74dadf8387
Understanding pandas.shape
March 6, 2025 - if df.shape[0] > 1000: print("Large dataset detected!")
Discussions

Difference between .shape[0] and .shape[1]
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 an array More on community.deeplearning.ai
🌐 community.deeplearning.ai
0
0
August 27, 2022
What does .isnull() and shape[0] do in Pandas?
https://pandas.pydata.org/docs/ More on reddit.com
🌐 r/learnpython
2
1
October 1, 2020
python - Understanding dataframe.shape df.shape - Stack Overflow
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 ... More on stackoverflow.com
🌐 stackoverflow.com
Python pandas - df.shape for a resultant dataframe that has no columns but index - Stack Overflow
Now the Data frame is actually empty since there are no columns at all, but on checking the df.shape ... Q-1 : Despite the dataframe is empty it says there are 10 rows. How to get the shape as (0,0) if the df is empty without doing explicit check of df.empty and manipulating shapes. More on stackoverflow.com
🌐 stackoverflow.com
🌐
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
🌐
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…
🌐
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.

🌐
W3Schools
w3schools.com › python › pandas › ref_df_shape.asp
Pandas DataFrame shape Property
Return the shape of the DataFrame: 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 ·
🌐
DataCamp
datacamp.com › tutorial › python-dataframe-size
Finding the Size of a DataFrame in Python | DataCamp
February 14, 2024 - Lastly, DataFrames contain an index, starting from 0, which allows you to select an individual element based on its position within the DataFrame. You can learn more about DataFrames in DataCamp’s data manipulation with pandas course or this Python pandas tutorial.
Find elsewhere
🌐
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'])
🌐
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
🌐
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.
🌐
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 - Get the number of rows and columns: df.shape · Get the number of rows: len(df) Get the number of columns: len(df.columns) Get the total number of elements: df.size · Notes when setting an index · Get the number of elements in a pandas.Series · Get the number of elements: len(s) and s.size · As an example, we will use the Titanic survivor dataset, which can be downloaded from Kaggle. import pandas as pd print(pd.__version__) # 2.0.0 df = pd.read_csv('data/src/titanic_train.csv') print(df.head()) # PassengerId Survived Pclass # 0 1 0 3 \ # 1 2 1 1 # 2 3 1 3 # 3 4 1 1 # 4 5 0 3 # # Name Sex Age SibSp # 0 Braund, Mr.
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)
🌐
Python Examples
pythonexamples.org › pandas-dataframe-shape
Pandas DataFrame.shape
For an empty DataFrame, the shape property returns (0, 0) if there are no rows and columns. import pandas as pd # Create an empty DataFrame df_empty = pd.DataFrame() # Get the shape of the empty DataFrame print("Shape of the Empty DataFrame:") print(df_empty.shape) Shape of the Empty 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)
🌐
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
🌐
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], ...
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 0.23.4 › generated › pandas.DataFrame.shape.html
pandas.DataFrame.shape — pandas 0.23.4 documentation
>>> df = pd.DataFrame({'col1': [1, 2], 'col2': [3, 4], ... 'col3': [5, 6]}) >>> df.shape (2, 3) index · modules | next | previous | pandas 0.23.4 documentation » · API Reference » ·
🌐
Pandas
pandas.pydata.org › 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