15

A quick question for the universe of programmers.

DATA. Data frame column consisting of names.

g['NAME']=['John', 'Michael', 'Jezus', 'Donald', 'Suzy']

DESIRED RESULT. Another, parallel data frame column consisting of number of characters in for each name in g['NAME'].

g['NAME_count'] = [4,7,5,6,4]

Thank you in advance!

1 Answers1

33

You can use vectorised str.len to achieve this:

In [106]:
df['NAME_Count'] = df['NAME'].str.len()
df

Out[106]:
      NAME  NAME_Count
0     John           4
1  Michael           7
2    Jezus           5
3   Donald           6
4     Suzy           4
EdChum
  • 376,765
  • 198
  • 813
  • 562