πŸ“œ I can make data readable in Python


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 Import

In
!pip install humanize
import humanize

​

πŸ’₯ Add commas to big numbers

In
n = 1234567890
print(humanize.intcomma(n))
print(humanize.intcomma(12345.67))
print(-humanize.intcomma(9876543))
Out
1,234,567,890
12,345.67
-9,876,543

​

♻️ Large numbers to units

In
print(humanize.intword(1250000))
print(humanize.intword(2000))
print(humanize.intword(987000000))
Out
1.2 million
2.0 thousand
987.0 million

​

πŸ† Ranks, leaderboards that make sense

In
for i in [1, 2, 3, 11, 21, 101]:
    print(i, "β†’", humanize.ordinal(i))
Out
1 β†’ 1st
2 β†’ 2nd
3 β†’ 3rd
11 β†’ 11th
21 β†’ 21st
101 β†’ 101st

​

πŸ“‚ File sizes that we understand

In
bytes_count = 3145728
print(humanize.naturalsize(bytes_count))
print(humanize.naturalsize(bytes_count, binary=True))
Out
3.1 MB
3.0 MiB

​

⏳ Once upon a time!

In
from datetime import datetime, timedelta
past = datetime.now() - timedelta(minutes=3, seconds=12)
print(humanize.naturaltime(past))
Out
3 minutes ago

​

πŸ—“οΈ Calendar labels

In
from datetime import date, timedelta
import humanize
d1 = date.today()
d2 = date.today() - timedelta(days=1)
d3 = date.today() - timedelta(days=45)
print(humanize.naturaldate(d1)) # 'today'
print(humanize.naturaldate(d2)) # 'yesterday'
print(humanize.naturaldate(d3))
Out
'today'
'yesterday'
Jul 25, 2025

​

🀏 Small numbers to words

Does only till 9

In
for i in range(0, 12):
    print(i, "β†’", humanize.apnumber(i))
Out
0 β†’ zero
1 β†’ one
2 β†’ two
3 β†’ three
4 β†’ four
5 β†’ five
6 β†’ six
7 β†’ seven
8 β†’ eight
9 β†’ nine
10 β†’ 10
11 β†’ 11

​

πŸ‹οΈ Mini exercise

  • Add thousands separators to 9876543.
  • Show a human-readable size for 10,485,760 bytes.
  • Display how long ago 7 minutes was.

β­πŸ“£ 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. 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...

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...