[英]Count numbers of chinese characters for each row of one column in Python
Given a dataframe as follows:给定一个数据框,如下所示:
id name
0 1 个体户
1 2 个人
2 3 利他润己企业管理有限公司
3 4 博通国际投资有限公司
4 5 西潼·科技有限公司
5 6 度咪科技有限公司
How could I count the numbers of chinese characters for each row of name
column?我如何计算
name
列每行的汉字数?
The expected result will be like this:预期的结果将是这样的:
id name count
0 1 个体户 3
1 2 个人 2
2 3 利他润己企业管理有限公司 12
3 4 博通国际投资有限公司 10
4 5 西潼科技有限公司 8
5 6 度咪科技有限公司 8
The following code works, but it will be appreciated if you could share other possible solutions.以下代码有效,但如果您能分享其他可能的解决方案,我们将不胜感激。
def hans_count(str):
hans_total = 0
for s in str:
if '\u4e00' <= s <= '\u9fef':
hans_total += 1
return hans_total
df['count'] = df['name'].apply(hans_count)
df
Out:出去:
id name count
0 1 个体户 3
1 2 个人 2
2 3 利他润己企业管理有限公司 12
3 4 博通国际投资有限公司 10
4 5 西潼科技有限公司 8
5 6 度咪科技有限公司 8
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.