Counting Characters of Words in a Series

Counting characters of words in a series is useful in many data analysis projects, by knowing the individual count and frequency, we can gain a better understanding of the patterns and trends of a text. In this thread, different simple and efficient methods will be discussed for counting characters of words in a series. If you want to learn how to capitalize each word in a series, you can go through the thread of Capitalize first character of elements in a series.

1. Using "len()" function with list comprehension:

  • The function len() is a built-in Python function that returns the length of an object. The object can be a string, list, tuple, dictionary, set, or any other iterable object.
  • List comprehension is used to apply len() to all values in the series and get a list which is then converted into a series again using pd.Series() before the final print.

2. Using "str.count()" method:

  • In Python, str.count() is a string method that returns the number of occurrences of a substring in the given string.
  • The pattern provided to the function is "\w" which is a regular expression pattern that matches alphanumeric characters and in this way, we consider both letters and digits in the count.

3. Using "s.str.len()" method:

  • The str.len() is a string method that returns the length of a string i.e., the number of characters in it and it can be directly applied to a series object.

4. Using lambda function with "apply()":

  • In this technique, a simple lambda function is used which returns the length of an item passed to it.
  • This lambda function is applied on the series object using the apply() method which is used to apply any function to a series or dataframe object.

Note: You can also create a different lambda function that performs the same task in another way.

5. Using "len()" function with "map()":

  • The function len() is a built-in Python function that returns the length of an object. The object can be a string, list, tuple, dictionary, set, or any other iterable object.
  • In Pandas, map() is a method that can be applied to a Pandas series or dataframe column to transform each element of the column using a Python function. In this technique, Python’s len() function is applied to each element using map().