dtoolkit.accessor.index.len#
- dtoolkit.accessor.index.len(index: Index, /, number: int = 1, other: int = None) Index [source]#
Return the length of each element in the Index.
Equals to
index.map(len)
, but the length ofnumber
type will as1
, the length of other types will asNaN
.- Parameters:
- numberint, default 1
The default length of number type.
- otherint or None, default None
The default length of other type.
- Returns:
- Index(int64)
See also
Notes
To keep the Python naming style, so use this accessor via
Index.len
rather thanIndex.lens
.Different to
pandas.Index.str.len()
. It only returnscollections.abc.Iterable
type length. Other type will return NaN.
Examples
>>> import dtoolkit >>> import pandas as pd >>> index = pd.Index([0, 1.5, "str", ("tuple",), ["list"], {}, object]) >>> index Index([0, 1.5, 'str', ('tuple',), ['list'], {}, <class 'object'>], dtype='object') >>> index.len() Index([1.0, 1.0, 3.0, 1.0, 1.0, 0.0, nan], dtype='float64')
Set number and other default return.
>>> index.len(number=0, other=0) Index([0, 0, 3, 1, 1, 0, 0], dtype='int64')