⁉️ Not many know that Python can analyze emojis πŸ˜€


Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python.

​

Amazon reviews, chats and social media text (posts, comments, stories etc) are full of emojis πŸ”₯πŸ”₯

Great for expression! But give a hard time for text interpretation (Amazon, Meta would be going crazy).

That is where Python's demoji library comes in. From a given text you can:

  • Extract emojis
  • Remove them if needed
  • Replace with textual meaning
  • Understand sentiment
  • and what not...

Let's begin!!

​

Install demoji library

Don't worry if you don't know "pip" for now

In:
!pip install demoji # install once in notebooks
Out:
Collecting demoji
Downloading demoji-1.1.0-py3-none-any.whl (42 kB)
Installing collected packages: demoji
Successfully installed demoji-1.1.0

​

Import like we do for other modules

In:
import demoji
demoji.download_codes() # download latest emoji description (run once)
Out: codes downloaded to …/demoji/codes.json

​

Find which emojis are present and their meaning

In:
text = "Great food πŸ˜‹ but service was slow πŸ˜•"
print(demoji.findall(text))
Out: {'πŸ˜‹': 'face savoring food', 'πŸ˜•': 'confused face'}

​

Replace emojis with their description

Emoji description can provide valuable info during text analysis

In:
desc = demoji.replace_with_desc("That goal was πŸ”₯πŸ”₯") # keep meaning in plain text
print(desc)
Out: That goal was :fire::fire:

​

Remove them completely

Used when we only want to focus on text

In:
clean = demoji.replace("Homework done πŸ˜…πŸ“š", "")
print(clean)
Out: Homework done

​

Find different types of emojis used

In:
count = len(demoji.findall("πŸ˜‚πŸ˜‚ That's hilarious!"))
print(count)
Out: 1

​

Get meaning of first emoji only

Capture emotion instantly

In:
text = "So happy today 😍"
meaning = list(demoji.findall(text).values())[0]
print(meaning)
Out: smiling face with heart-eyes

​

Convert emoji to sentiment

In:
text = "Movie was πŸ‘ but ending was 😑"
mapped = demoji.replace_with_desc(text)
mapped = mapped.replace(":thumbs up:", ":positive:")
mapped = mapped.replace(":pouting face:", ":negative:")
print(mapped)
Out: Movie was :positive: but ending was :negative:

​

Find if text has emoji

In:
text = "Refund now!!! 😑"
has_emoji = bool(demoji.findall(text))
print(has_emoji)
Out: True

​

Extract all emojis

In:
emojis = "".join(demoji.findall("Trip was great πŸ–οΈπŸΉβœˆοΈ").keys())
print(emojis)
Out: πŸ–οΈπŸΉβœˆοΈ

β­πŸ“£ That's it for today! If you liked it, please share it with anyone who will find it useful 🐼🐼

​

Pandas Daily

Beginner to Expert in Python in just 5 minutes

Read more from Pandas Daily

Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. We all agree that dashboards, emails, logs should read like English. Meaning 1.2 million beats 1234567 and 3.1M is more readable than 3145728. Stakeholders scan - and they get pissed if they have to parse messy numbers 😭😭 So today, we explore a third-party humanize library. Format numbers, file sizes, and dates/times into human-readable text. Mini exercise at the end. πŸ₯‡ Ready, Get, Set, Go! Install and...

Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. We all know Python’s basic container data types β€” lists, dictionaries, tuples, sets. "Container" is just a data type that holds multiple items instead of single values. However, many times basics are not enough. Like if you want count of different items in the list you will run a loop like below. In: fruits = ["apple", "banana", "apple", "cherry"] counts = {} for item in fruits: counts[item] =...

Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. Do you want to outsmart your friends (and interviewers) with Python? Couple of weeks back we played around with date and time using datetime module. Guess what - Python has another super handy module: calendar. Allows you to do all things related to calendars - like literally printing it in a readable form too. And its extremely simple to learn! Yet not many do it πŸ˜” Lets Go πŸš€πŸš€ ✨ Import calendar module...