w3resource

Python Pandas: Calculate the number of characters in each word in a given series

Python Pandas: Data Series Exercise-25 with Solution

Write a Pandas program to calculate the number of characters in each word in a given series.

Sample Solution :

Python Code :

import pandas as pd
series1 = pd.Series(['Php', 'Python', 'Java', 'C#'])
print("Original Series:")
print(series1)
result = series1.map(lambda x: len(x))
print("\nNumber of characters in each word in the said series:")
print(result)

Sample Output:

Original Series:
0       Php
1    Python
2      Java
3        C#
dtype: object

Number of characters in each word in the said series:
0    3
1    6
2    4
3    2
dtype: int64

Python Code Editor:


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Pandas program convert the first and last character of each word to upper case.
Next: Write a Pandas program to compute difference of differences between consecutive numbers of a given series.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Python: Membership Testing in a Collection

>>> a = ('one', 'two', 'three', 'four', 'five')
>>> if 'one' in a:
...     print('The tuple contains one.')
... 
The tuple contains one.
>>> b = {0: 'zero', 1: 'one', 2: 'two', 3: 'three'}
>>> if 2 in b.keys():
...     print('The dict has the key of 2.')
... 
The dict has the key of 2.