Extract emoji from series of text
Asked Answered
K

1

2

I have problem extracting emoji from a series. The code used:

import emoji
def extract_emojis(text):
  return ''.join(c for c in text if c in emoji.UNICODE_EMOJI)

for text in df['comments']:
    df['emoji']=extract_emojis(text)

Output:

             comments                                    | emoji
0     Its very beautiful    
1   Your new bike, @keir ...?   
2   @philip 🤩🤩    
3   Any news on the Canadian expansion mentioned i...   
4   Rocky Mountain ❤️   
... ... ...

Checking the function on just a text:

text = '@philip 🤩🤩'
extract_emojis(text)
--> '\U0001f929\U0001f929'        

Expected result:

             comments                                    | emoji
0     Its very beautiful                                 |
1   Your new bike, @keir ...?                            |
2   @philip 🤩🤩                                         | 🤩🤩
3   Any news on the Canadian expansion mentioned i...    |
4   Rocky Mountain ❤️                                    | ❤️ 
... ... ...

Note: I have only asked this question after looking at these links:
Python unicode character conversion for Emoji
How to extract all the emojis from text?

Kele answered 6/9, 2020 at 9:20 Comment(2)
What is in s?Doable
I have edited the post and the code, same result @DoableKele
L
1

Rather than iterating over the entire dataset. You can apply the function using apply or lambda.

import pandas as pd 
import emoji
df = pd.DataFrame([['@philip 🤩🤩 '],
['Rocky Mountain ❤️']],columns = ['comments'])

Using Lambda:

df['emojis'] = df['comments'].apply(lambda row: ''.join(c for c in row if c in emoji.UNICODE_EMOJI))
df

using Apply

def extract_emojis(text):
    return ''.join(c for c in text if c in emoji.UNICODE_EMOJI)

df['emoji_apply'] = df['comments'].apply(extract_emojis)
df

Output:

comments    emojis
@philip 🤩🤩    🤩🤩
Rocky Mountain ❤️   ❤
Lambrequin answered 6/9, 2020 at 9:51 Comment(3)
thanks a lot, but just to increase my understanding, could you tell me why the for loop return empty results?Kele
@Kele When you do df['column'] = some_value it sets some_value to the entire column. In your case you will see that last executed statement's result is populated in the df.Lambrequin
Spend 2 hours trying different solutions with many votes. None worked. Only this. Thanks!!Mcnamee

© 2022 - 2024 — McMap. All rights reserved.