Pandas Series.str.len() Method



The Series.str.len() method in Python Pandas library is useful for calculating the length of each element in a Series or Index. The elements can be sequences such as strings, tuples, or lists, as well as collections like dictionaries.

This method is particularly useful for text data processing and analysis, as it allows you for quick and efficient calculation of the size or length of the elements within the Series.

Syntax

Following is the syntax of the Pandas Series.str.len() method:

Series.str.len()

Parameters

The Series.str.len() method does not accept any parameters.

Returns

The Series.str.len() method returns a Series or Index of integers. Each integer value indicates the length of the corresponding element in the original Series or Index.

Example 1

This example demonstrates the basic usage of the Series.str.len() method to compute the length of string elements in a Series.

import pandas as pd

# Create a Series of strings
series = pd.Series(["Foo", "bar", "London", "Quarantine"])
print("Series: \n", series)

# Compute the length of each string element
length = series.str.len()
print("Length:\n", length)

When we run the above code, it produces the following output:

Series: 
 0           Foo
1           bar
2        London
3    Quarantine
dtype: object
Length:
 0    3
1    3
2    6
3    10
dtype: int64

Example 2

This example demonstrates the Series.str.len() method with a Series containing different types of elements, including strings, empty strings, integers, dictionaries, lists, and tuples.

import pandas as pd

# Create a Series with various types of elements
s = pd.Series(['panda', '', 5, {'language': 'Python'}, [2, 3, 5, 7], ('one', 'two', 'three')])
print("Original Series: \n", s)

# Compute the length of each element
length = s.str.len()

print("Output Length of each element in the Series:\n", length)

When we run the above code, it produces the following output:

Original Series: 
 0                     panda
1                          
2                         5
3    {'language': 'Python'}
4              [2, 3, 5, 7]
5         (one, two, three)
dtype: object

Output Length of each element in the Series:
 0    5.0
1    0.0
2    NaN
3    1.0
4    4.0
5    3.0
dtype: float64

Example 3

This example demonstrates the use of the Series.str.len() method with a DataFrame containing lists in one of its columns. The length of each list is computed and stored in a new column.

import pandas as pd

# Create a DataFrame with a column of lists
df = pd.DataFrame({"A": [[1, 2, 3], [0, 1, 3]], "B": ['Tutorial', 'AEIOU']})

print("Original DataFrame:")
print(df)

# Compute the length of each element in column 'B'
df['C'] = df['B'].str.len()

print("\nDataFrame after computing the length of elements in column 'B':")
print(df)

When we run the above code, it produces the following output:

Original DataFrame:
           A         B
0  [1, 2, 3]  Tutorial
1  [0, 1, 3]     AEIOU

DataFrame after computing the length of elements in column 'B':
           A         B  C
0  [1, 2, 3]  Tutorial  8
1  [0, 1, 3]     AEIOU  5
Advertisements