w3resource

Pandas Series: str.len() function

Series-str.len() function

The str.len() function is used to compute the length of each element in the Series/Index.

Compute the length of each element in the Series/Index. The element may be a sequence (such as a string, tuple or list) or a collection (such as a dictionary).

Syntax:

Series.str.len(self)
Pandas Series: str.len() function

Returns: Series or Index of int
A Series or Index of integer values indicating the length of each element in the Series or Index.

Example - Returns the length (number of characters) in a string. Returns the number of entries for dictionaries, lists or tuples:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(['fox',
                '',
                6,
               {'f1' : 'b1'},
               [3, 5, 7, 9],
               ('one', 'two', 'three')])
s

Output:

0                  fox
1                     
2                    6
3         {'f1': 'b1'}
4         [3, 5, 7, 9]
5    (one, two, three)
dtype: object

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(['fox',
                '',
                6,
               {'f1' : 'b1'},
               [3, 5, 7, 9],
               ('one', 'two', 'three')])
s.str.len()

Output:

0    3.0
1    0.0
2    NaN
3    1.0
4    4.0
5    3.0
dtype: float64

Previous: Series-str.join() function
Next: Series-str.lower() function



Follow us on Facebook and Twitter for latest update.