pandas 计算字符串中的字符数,从中创建一个数据框列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41243263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:39:07  来源:igfitidea点击:

Count number of characters in a string, create a data frame column out of it?

pythonstringpandasdataframecount

提问by Gediminas Sadaunykas

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'] 中每个名称的字符数组成。

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

Thank you in advance!

先感谢您!

回答by EdChum

You can use vectorised str.lento achieve this:

您可以使用矢量化str.len来实现这一点:

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