size comes from numpy (on which pandas is based).

It gives you the total number of elements in the array. However, you can also query the sizes of specific axes with np.size (see below).

In contrast, len gives the length of the first dimension.

For example, let's create an array with 36 elements shaped into three dimensions.

In [1]: import numpy as np                                                      

In [2]: a = np.arange(36).reshape(2, 3, -1)                                     

In [3]: a                                                                       
Out[3]: 
array([[[ 0,  1,  2,  3,  4,  5],
        [ 6,  7,  8,  9, 10, 11],
        [12, 13, 14, 15, 16, 17]],

       [[18, 19, 20, 21, 22, 23],
        [24, 25, 26, 27, 28, 29],
        [30, 31, 32, 33, 34, 35]]])

In [4]: a.shape                                                                 
Out[4]: (2, 3, 6)

size

size will give you the total number of elements.

In [5]: a.size                                                        
Out[5]: 36

len

len will give you the number of 'elements' of the first dimension.

In [6]: len(a)                                                                  
Out[6]: 2

This is because, in this case, each 'element' stands for a 2-dimensional array.

In [14]: a[0]                                                                   
Out[14]: 
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17]])

In [15]: a[1]                                                                   
Out[15]: 
array([[18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34, 35]])

These arrays, in turn, have their own shape and size.

In [16]: a[0].shape                                                             
Out[16]: (3, 6)

In [17]: len(a[0])                                                              
Out[17]: 3

np.size

You can use size more specifically with np.size.

For example you can reproduce len by specifying the first ('0') dimension.

In [11]: np.size(a, 0)                                                          
Out[11]: 2

And you can also query the sizes of the other dimensions.

In [10]: np.size(a, 1)                                                          
Out[10]: 3

In [12]: np.size(a, 2)                                                          
Out[12]: 6

Basically, you reproduce the values of shape.

Answer from nocibambi on Stack Overflow
🌐
CASRAI
casrai.org › guides › research tools & software › statistical software for research › numpy ndarray length: len(), .shape and .size explained
NumPy ndarray Length: len, shape, size — CASRAI
2 weeks ago - The NumPy documentation states the relationship between the first two directly: len(a) is equivalent to np.shape(a)[0] for N-dimensional arrays with N ≥ 1, and ndarray.size is the number of elements in the array, equal to np.prod(a.shape). ...
Top answer
1 of 3
17

size comes from numpy (on which pandas is based).

It gives you the total number of elements in the array. However, you can also query the sizes of specific axes with np.size (see below).

In contrast, len gives the length of the first dimension.

For example, let's create an array with 36 elements shaped into three dimensions.

In [1]: import numpy as np                                                      

In [2]: a = np.arange(36).reshape(2, 3, -1)                                     

In [3]: a                                                                       
Out[3]: 
array([[[ 0,  1,  2,  3,  4,  5],
        [ 6,  7,  8,  9, 10, 11],
        [12, 13, 14, 15, 16, 17]],

       [[18, 19, 20, 21, 22, 23],
        [24, 25, 26, 27, 28, 29],
        [30, 31, 32, 33, 34, 35]]])

In [4]: a.shape                                                                 
Out[4]: (2, 3, 6)

size

size will give you the total number of elements.

In [5]: a.size                                                        
Out[5]: 36

len

len will give you the number of 'elements' of the first dimension.

In [6]: len(a)                                                                  
Out[6]: 2

This is because, in this case, each 'element' stands for a 2-dimensional array.

In [14]: a[0]                                                                   
Out[14]: 
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17]])

In [15]: a[1]                                                                   
Out[15]: 
array([[18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34, 35]])

These arrays, in turn, have their own shape and size.

In [16]: a[0].shape                                                             
Out[16]: (3, 6)

In [17]: len(a[0])                                                              
Out[17]: 3

np.size

You can use size more specifically with np.size.

For example you can reproduce len by specifying the first ('0') dimension.

In [11]: np.size(a, 0)                                                          
Out[11]: 2

And you can also query the sizes of the other dimensions.

In [10]: np.size(a, 1)                                                          
Out[10]: 3

In [12]: np.size(a, 2)                                                          
Out[12]: 6

Basically, you reproduce the values of shape.

2 of 3
0

Numpy nparray has Size https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.size.html

Whilst len is from Python itself

Size is from numpy ndarray.size

The main difference is that nparray size only measures the size of an array, whilst python's Len can be used for getting the length of objects in general

🌐
Medium
medium.com › @heyamit10 › numpy-ndarray-size-931a5805aedb
Understanding ndarray.size. If you think you need to spend $2,000… | by Hey Amit | Medium
February 8, 2025 - len() gives you the length of the first dimension (number of rows in a 2D array). .size gives you the total number of elements in the array.
People also ask

What is the difference between numpy ndarray size and length?
Length ( len() ) is the number of items along the first axis. Size ( .size ) is the number of elements across all axes, equal to np.prod(a.shape) . A 3 x 5 x 2 array has length 3 and size 30.
🌐
casrai.org
casrai.org › guides › research tools & software › statistical software for research › numpy ndarray length: len(), .shape and .size explained
NumPy ndarray Length: len, shape, size — CASRAI
What does numpy.ndarray length return for a 2-D array?
The number of rows. A 2 x 3 array returns 2; use a.shape[1] for the 3 columns or a.size for all 6 elements.
🌐
casrai.org
casrai.org › guides › research tools & software › statistical software for research › numpy ndarray length: len(), .shape and .size explained
NumPy ndarray Length: len, shape, size — CASRAI
🌐
Leapcell
leapcell.io › blog › understanding-array-length-in-python
Understanding Array Length in Python | Leapcell
July 25, 2025 - Here, len(np_array) returns the size of the first dimension (i.e., the number of rows), while .size gives the total number of elements in the entire array.
🌐
Runebook.dev
runebook.dev › en › docs › numpy › reference › generated › numpy.ndarray.__len__
NumPy's len() vs. .shape and .size: A Guide to Array Dimensions
The .size attribute gives you the product of all dimensions, which is the total element count. Sometimes you need the length of a dimension other than the first one (e.g., the number of columns in a 2D array). Using len() won't work for this.
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: Get the dimensions, shape, and size of an array | note.nkmk.me
April 23, 2025 - For a numpy.ndarray, len() returns the size of the first dimension, which is equivalent to shape[0]. It is also equal to size only for one-dimensional arrays.
🌐
IONOS
ionos.com › digital guide › websites › web development › python array length
How to find out the length of a Python array - IONOS
July 11, 2023 - When you look at the l variable, you can see that there is no dif­fer­ence to len. This is because, in this case, the variable l contains six elements. However, there is a surprise with size. With the s variable, we find the number 12. This is because size counts the elements of all arrays in­di­vid­u­al­ly.
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to determine the length of a numpy array
5 Best Ways to Determine the Length of a NumPy Array - Be on the Right Side of Change
February 20, 2024 - The snippet uses the np.size() function on a 3×2 array without specifying an axis, which results in the total number of elements in the array being calculated and printed. Method 1: size Attribute. Provides total element count. Simple to use but not axis-specific. Method 2: shape and Indexing. Allows axis-specific sizes. Requires indexing, which might be less intuitive for total size. Method 3: len() Function.
Find elsewhere
🌐
Python Guides
pythonguides.com › python-numpy-shape
NumPy Shape in Python: shape[0] and shape[1]
May 8, 2025 - On a 3 by 4 array it returns 3 and says nothing whatsoever about the columns, so a silent bug is easy to write. size is the product of the shape tuple, which is why an array built with np.zeros and one built with np.empty report the same size ...
🌐
Appdividend
appdividend.com › python-array-length
How to Get the Length of an Array in Python
September 7, 2025 - If you are working with a list or array's array structure, use the len() function. For numpy arrays, use the .size attribute.
🌐
Python Guides
pythonguides.com › check-if-numpy-array-is-empty
How to Check if a NumPy Array Is Empty (arr.size)
May 6, 2025 - Use arr.size == 0 or np.size(arr) ... is ambiguous”. Recent versions also raise for empty arrays. Only for 1-D arrays. len() counts rows, so a 3×0 array has len() 3 but no elements....
🌐
Codegive
codegive.com › blog › numpy_length_of_array.php
Numpy length of array
Is its "length" 6 (total number of elements)? NumPy provides distinct tools to answer each of these questions precisely. Generally, when len() is applied to a multi-dimensional NumPy array, it refers to the size of the first dimension (i.e., the number of "outermost" elements or rows).
Top answer
1 of 5
23

I wouldn't worry about performance here - any differences should only be very marginal.

I'd say the more pythonic alternative is probably the one which matches your needs more closely:

a.shape may contain more information than len(a) since it contains the size along all axes whereas len only returns the size along the first axis:

>>> a = np.array([[1,2,3,4], [1,2,3,4]])
>>> len(a)
2
>>> a.shape
(2L, 4L)

If you actually happen to work with one-dimensional arrays only, than I'd personally favour using len(a) in case you explicitly need the array's size.

2 of 5
12

From the source code, it looks like shape basically uses len(): https://github.com/pandas-dev/pandas/blob/master/pandas/core/frame.py

@property
def shape(self) -> Tuple[int, int]:
    return len(self.index), len(self.columns)
def __len__(self) -> int:
    return len(self.index)

Calling shape will attempt to run both dim calcs. So maybe df.shape[0] + df.shape[1] is slower than len(df.index) + len(df.columns). Still, performance-wise, the difference should be negligible except for a giant giant 2D dataframe.

So in line with the previous answers, df.shape is good if you need both dimensions, for a single dimension, len() seems more appropriate conceptually.

Looking at property vs method answers, it all points to usability and readability of code. So again, in your case, I would say if you want information about the whole dataframe just to check or for example to pass the shape tuple to a function, use shape. For a single column, including index (i.e. the rows of a df), use len().

🌐
RunxBuild
runxbuild.com › home › blog › python array length: len is the answer, and python lists are not arrays
Python Array Length: len Is the Answer, and Python Lists Are Not Arrays
July 18, 2026 - For a plain list, len is the whole story. For NumPy, len gives only the outer dimension, so reach for .shape or .size when you mean total elements.
🌐
Python Guides
pythonguides.com › check-the-length-of-an-array-in-python
How To Check The Length Of An Array In Python?
July 22, 2026 - NumPy’s .size property counts every element across all dimensions. For nested lists and multidimensional NumPy arrays, len() measures the outermost container.
🌐
Udacity
udacity.com › blog › 2021 › 10 › how-to-calculate-the-length-of-an-array-in-python.html
How to calculate the length of an array in Python? | Udacity
October 20, 2021 - You can use the len() method for NumPy arrays, but NumPy also has the built-in typecode .size that you can use to calculate length.
🌐
Programmersought
programmersought.com › article › 13562697677
The difference between the usage len, size, shape of - Programmer Sought
numpy shape is built-in function, ... print(a.shape) (2, 3) print(a.shape[0]) 2 · numpy size is built-in function, it returns the number of one dimension of the array elements, if not specified dimensions, number of all elements are summed....
🌐
Programmersought
programmersought.com › article › 3539725621
The difference between size, shape and len in python-numpy - Programmer Sought
Numpy array type The output is: For Numpy Array, Shape, Size is the properties of Array; Pytorch Tensor Type The output is: For Tensor, Shape is the properties of Tensor; Size () is a method of tensor... len() Can be used to view the length of an array or slice cap()Can be used to view the ...