Python Practice › Pandas Programs › Program 26

Length of each word

EasyPandas Programs · 26 of 95

Write a pandas program to count the characters of each word.

Advertisement
Input
["Virat", "Rohit", "Pant", "Shikhar"]
Output
0    5
1    5
2    4
3    7
dtype: int64

Approach

  1. str.len().

Solution

Python
import pandas as pdprint(pd.Series(["Virat", "Rohit", "Pant", "Shikhar"]).str.len())
Output
0    5
1    5
2    4
3    7
dtype: int64
Note

“Virat” and “Rohit” have 5 letters; the original sample showed 6.

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More pandas programs

← All 95 Pandas Programs