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
- Import pandas library as pd.
- Create a series using pd.Series().
- Calculate the number of characters in each word in a series using df.map(lambda x: len(x)).
- Pass the word of the series to the lambda function using df.map().
- Use len() to calculate the number of characters in each word.
- 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