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.
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.
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
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.
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().
python - What is faster: Python3's 'len' or numpys shape? - Stack Overflow
Is it ever advantageous to use a standard Python list vs a numpy array when all elements are the same type?
I want to know the length of a two-dimensional array
Difference between .nunique() and .unique() please
Does len() work on a NumPy array?
Why is it len(x) and not x.length in Python?
Is len() slow on a large list?
And if so, which is better to use?
def time_compare(self):
loops = 100000000
start = time.time()
for i in range(loops):
self.value_map.shape[0]
self.value_map[0].shape[0]
self.value_map[0][0].shape[0]
self.value_map[0][0][0].shape[0]
end = time.time()
timed = (end - start)
print("shape={}".format(timed))
start = time.time()
for i in range(loops):
len(self.value_map)
len(self.value_map[0])
len(self.value_map[0][0])
len(self.value_map[0][0][0])
end = time.time()
timed = (end - start)
print("len={}".format(timed))
shape=102.26551818847656
len=87.99720764160156
len is faster than shape
The normal numpy equivalent would be
np.ones((points.shape[0],1))*3
shape is an attribute of an array, so accessing it is essentially instantaneous. It doesn't have to do any calculating.
In [277]: points.shape
Out[277]: (18, 1, 2)
In [278]: points.size # number of elements
Out[278]: 36
In [279]: len(points) # size of the 1st dimension
Out[279]: 18
In the above np.ones... expression, the shape or len() is a very small part of the computation time. It doesn't matter which you use. But shape is more general, eg. np.ones(points.shape[:2]) would give the same (18,1) array.