1

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?

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
Shaido
  • 27,497
  • 23
  • 70
  • 73
ah bon
  • 9,293
  • 12
  • 65
  • 148

2 Answers2

6

You can use str.count to do this together with a regex pattern:

df['count'] = df['name'].str.count(pat='[\u4e00-\u9fff]')

Result:

   id                    name   count
0   1                   个体户      3
1   2                    个人       2
2   3  利他润己企业管理有限公司      12
3   4      博通国际投资有限公司      10
4   5        西潼·科技有限公司       8
5   6         度咪科技有限公司       8
Shaido
  • 27,497
  • 23
  • 70
  • 73
  • 1
    The regex obviously selects just a slice of Unicode which seems suitable for this particular problem statement, but might not work well for related problems. Unfortunately, Python's `re` library currently does not support Unicode properties; with e.g. Perl you could say `\p{Letter}` to select "letter" code points in any script (not just Chinese) but Python doesn't currently have support for this in the standard library. Perhaps see also https://stackoverflow.com/questions/1366068 for a more complete enumeration of Chinese script ranges in Unicode. – tripleee Dec 28 '20 at 09:53
0

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
ah bon
  • 9,293
  • 12
  • 65
  • 148
  • @Shaido can you please advice me with this https://stackoverflow.com/questions/66167061/transposing-table-to-given-format-in-spark – BdEngineer Feb 12 '21 at 06:07