Open In App

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

Last Updated : 28 Jul, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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:
length of words in series

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:
length of words in series-2

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:
length of words in series-4



Similar Reads

Pandas Series dt.daysinmonth | Get Number of Days in Month in Pandas Series
The dt.daysinmonth attribute returns the number of days in the month for the given DateTime series object. Example C/C++ Code import pandas as pd sr = pd.Series(['2012-12-31', '2019-1-1 12:30', '2008-02-2 10:30', '2010-1-1 09:25', '2019-12-31 00:00']) idx = ['Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5'] sr.index = idx sr = pd.to_datetime(sr) result
2 min read
Pandas - Convert the first and last character of each word to upper case in a series
In python, if we wish to convert only the first character of every word to uppercase, we can use the capitalize() method. Or we can take just the first character of the string and change it to uppercase using the upper() method. So, to convert the first and last character of each word to upper case in a series we will be using a similar approach. F
2 min read
Python | Pandas Series.mad() to calculate Mean Absolute Deviation of a Series
Pandas provide a method to make Calculation of MAD (Mean Absolute Deviation) very easy. MAD is defined as average distance between each value and mean. The formula used to calculate MAD is: Syntax: Series.mad(axis=None, skipna=None, level=None) Parameters: axis: 0 or ‘index’ for row wise operation and 1 or ‘columns’ for column wise operation. skipn
2 min read
Calculate the frequency counts of each unique value of a Pandas series
Let us see how to find the frequency counts of each unique value of a Pandas series. We will use these methods to calculate the frequency counts of each unique value of a Pandas series. Using values_counts() to calculate the frequency of unique value Here values_counts() function is used to find the frequency of unique value in a Pandas series. Exa
3 min read
Pandas Series dt.weekofyear Method | Get Week of Year in Pandas Series
The dt.weekofyear attribute returns a Series containing the week ordinal of the year in the underlying data of the given series object. Example C/C++ Code import pandas as pd sr = pd.Series(['2012-10-21 09:30', '2019-7-18 12:30', '2008-02-2 10:30', '2010-4-22 09:25', '2019-11-8 02:22']) idx = ['Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5'] sr.index =
2 min read
Pandas Series dt.minute | Extract Minute from DateTime Series in Pandas
Pandas Series.dt.minute attribute returns a NumPy array containing the minutes of the DateTime in the underlying data of the given series object. Example C/C++ Code import pandas as pd sr = pd.Series(['2012-10-21 09:30', '2019-7-18 12:30', '2008-02-2 10:30', '2010-4-22 09:25', '2019-11-8 02:22']) idx = ['Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5']
2 min read
Pandas Series dt.dayofweek | Get Day of Week from DateTime Series in Pandas
Pandas dt.dayofweek attribute returns the day of the week from the given DateTime Series Object. It is assumed the week starts on Monday, which is denoted by 0, and ends on Sunday which is denoted by 6. Example: C/C++ Code import pandas as pd sr = pd.Series(['2012-10-21 09:30', '2019-7-18 12:30', '2008-02-2 10:30', '2010-4-22 09:25', '2019-11-8 02:
2 min read
Pandas Series dt.freq | Retrieve Frequency of Pandas Time Series
Pandas dt.freq attribute returns the time series frequency applied on the given series object if any, else it returns None. Examples C/C++ Code import pandas as pd sr = pd.Series(['2012-12-31', '2019-1-1 12:30', '2008-02-2 10:30', '2010-1-1 09:25', '2019-12-31 00:00']) idx = ['Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5'] sr.index = idx sr = pd.to_da
2 min read
Pandas Series dt.normalize() | Normalize Time in Pandas Series
The dt.normalize() method converts times to midnight. The time component of the date-time is converted to midnight i.e. 00:00:00. This is useful in cases when the time does not matter. Length is unaltered. The time zones are unaffected. Example: C/C++ Code import pandas as pd sr = pd.Series(pd.date_range('2012-12-31 09:45', periods = 5, freq = 'M',
2 min read
Pandas Series dt.week | Extract Week Number from DateTime Series
Pandas dt.week attribute returns a NumPy array containing the week ordinal of the year in the underlying data of the given DateTime Series object. Example C/C++ Code import pandas as pd sr = pd.Series(['2012-10-21 09:30', '2019-7-18 12:30', '2008-02-2 10:30', '2010-4-22 09:25', '2019-11-8 02:22']) idx = ['Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5']
2 min read