Finding the length of words in a given Pandas series

PythonPandasServer Side ProgrammingProgramming

In this task, we will find the length of strings in a Pandas series. We will use the str.len() function in the Pandas library for this purpose.

Algorithm

Step 1: Define a Pandas series of string.
Step 2: Find the length of each string using the str.len() function.
Step 3: Print the results.

Example Code

import pandas as pd

series = pd.Series(["Foo", "bar", "London", "Quarantine"])
print("Series: \n", series)

length = series.str.len()
print("Length:\n", length)

Output

Series:
0           Foo
1           bar
2        London
3    Quarantine
dtype: object
Length:
0     3
1     3
2     6
3    10
dtype: int64
raja
Published on 16-Mar-2021 10:56:58
Advertisements