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

In this python pandas program, we will calculate the number of characters in each word in a series using pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a series using pd.Series().
  3. Calculate the number of characters in each word in a series using df.map(lambda x: len(x)).
  4. Pass the word of the series to the lambda function using df.map().
  5. Use len() to calculate the number of characters in each word.
  6. Print the output.
				
					import pandas as pf
df = pd.Series(['virat','rohit','pant','shikhar'])
print("Original Series:")
print(df)
result = df.map(lambda x: len(x))
print("Number of characters in each word of series:")
print(result)
				
			

Output :

				
					Original Series:
0      virat
1      rohit
2       pant
3    shikhar
dtype: object
Number of characters in each word of series:
0    5
1    5
2    4
3    7
dtype: int64
				
			

convert the first and last character of each word to upper case in each word of a given series

convert a series of date strings to a time-series

Leave a Comment