Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
272 views
in Technique[技术] by (71.8m points)

python - Error: nothing to repeat when counting occurences in dataframe

I try to count the occurences of each emoji (in the emoji library) in my dataframe. My approach:

emoji_cnt = [[] for i in range(len(list(emoji.UNICODE_EMOJI.keys())))]

j = 0
for key, value in emoji.UNICODE_EMOJI.items():
    emoji_cnt[j].append(key)
    j = j+1

for k in emoji_cnt: 
    s = df["Message"].str.count(k[0]).sum()
    k.append(s)

actually works. I tried to print the numbers out (within in the Loop) and it works. But some how the programm stops and I get the following Error:

  File "C:UsersUSERanaconda3libsre_parse.py", line 668, in _parse
    raise source.error("nothing to repeat",

error: nothing to repeat

I am using Spyder on Anaconda and Python 3.8.

Thank you very much guys :)

Kind regards from Vienna!

question from:https://stackoverflow.com/questions/65848682/error-nothing-to-repeat-when-counting-occurences-in-dataframe

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

So found a way. I had to use thge module for regular expressions. so:

import re
.
.
.
emoji_cnt = [[] for i in range(len(list(emoji.UNICODE_EMOJI.keys())))]

j = 0
for key, value in emoji.UNICODE_EMOJI.items():
    emoji_cnt[j].append(key)
    j = j+1

for k in emoji_cnt: 
    s = df["Message"].str.count(re.escape(k[0])).sum()
    k.append(s)

re.escape(k[0]) will change the Emoji in k[0] into an regular expression.

:)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...