# Calculate the number of characters in each word in a Pandas series

## Calculate the number of characters in each word in a Pandas series

To calculate the numbers of characters we use Series.str.len(). This function returns the count of the characters in each word in a series.

Syntax: Series.str.len()

Return type: Series of integer values. NULL values might be present too depending upon caller series.

Another way to find the number of characters by using len() function (that wrapped inside the map function) so that the given series substitutes the value of length by taking series of data as input using pandas.series().

Let’s see some examples:

Example 1: We take input of words and count each character of words with the help of Series.map() that substitute the values and gives output using a function named as calc.

 `# Import pandas library` `import` `pandas as pd` ` ` `# Input series of words` `words ``=` `pd.Series([``'Java'``, ``'Kotlin'``,` `                   ``'Python'``, ``'Scala'``,` `                    ``'Ruby'``])` `print``(``"Given Series:"``)` `print``(words)` ` ` `# Substituting values using map` `rst ``=` `words.``map``(``lambda` `calc: ``len``(calc))` `print``(``"No. of characters in each word in the given series:"``)` `print``(rst)`

Output:

Example 2: We input a series of words and substitute values using Series.map() and use Series.apply() for single values.

 `# Import pandas library` `import` `pandas as pd` ` ` `# Input series of words` `words ``=` `pd.Series([``'Number'``, ``'Of'``, ``'Characters'``,` `                   ``'In'``, ``'Each'``, ``'Word'``])[``len``(count)` `                   ``for` `count ``in` `words]` ` ` `# Substituting values using a map` `words.``map``(``len``)` ` ` `# For single values` `words.``apply``(``len``)`

Output:

Example 3: To print in a specific format of word with its length.

 `# Import pandas library` `import` `pandas as pd` ` ` `# Input series of words` `words ``=` `pd.Series([``'alphabet'``, ``'consonants'``, ` `                   ``'vowels'``, ``'letters'``])` ` ` `# Display length of words` `# along with words` `for` `i ``in` `range``(``len``(words)):` `    ``print``(words[i], words.``str``.``len``()[i])     `

Output:

Last Updated on October 23, 2021 by admin

## Get the Hour from timestamp in PandasGet the Hour from timestamp in Pandas

Get the Hour from timestamp in Pandas Let’s see how to extract the hour from

## Replace Negative Number by Zeros in Pandas DataFrameReplace Negative Number by Zeros in Pandas DataFrame

Replace Negative Number by Zeros in Pandas DataFrame In this article, Let’s discuss how to

## Count distinct in Pandas aggregationCount distinct in Pandas aggregation

Count distinct in Pandas aggregation In this article, let’s see how we can count distinct

## Loop or Iterate over all or certain columns of a dataframe in Python – PandasLoop or Iterate over all or certain columns of a dataframe in Python – Pandas

Loop or Iterate over all or certain columns of a dataframe in Python-Pandas In this

## Python – Pandas Dataframe.sample()Python – Pandas Dataframe.sample()

Python Pandas Dataframe.sample()   Pandas sample() is used to generate a sample random row or