Q#84: User messaging
Below is a table schema for a new messaging app. The table contains send/receive message data for the application's users.
table name: user_messaging
time_stamp (integer) # timestamp, epoch seconds
sender_id (integer) # id of the message sender
receiver_id (integer) # id of the message receiver
Question: Using Python What fraction of active users communicate with at least 25 unique people on April 2nd, 2018?
TRY IT YOURSELF
ANSWER
Data Science is all about discovering and evaluating how businesses are running. More often than not you will be asked to address specific questions like this in order to create a business strategy moving forward.
To answer this question let’s pretend we have the data already loaded into a dataframe and utilize the Pandas library to answer the question.
import pandas as pd
# Read the data into a pandas DataFrame
data = pd.read_csv(<filename>)
# Step 1: Identifying Active Users
active_users = data[data['time_stamp'] >= '2018-03-03']
# Step 2: Filtering Messages on April 2nd, 2018
april_2nd_messages = active_users[active_users['time_stamp'].dt.date == pd.to_datetime('2018-04-02')]
# Step 3: Counting Unique Contacts for Each User
unique_contacts = april_2nd_messages.groupby('sender_id')['receiver_id'].nunique()
# Step 4: Determining the Fraction of Active Users
users_with_25plus_contacts = (unique_contacts >= 25).sum()
fraction_active_users = users_with_25plus_contacts / len(active_users)
# Print the result
print(f"The fraction of active users with 25+ unique contacts on April 2nd, 2018 is: {fraction_active_users:.2%}")
Plug: Please purchase my book ONLY if you have the means to do so. Imagination Unleashed: Canvas and Color, Visions from the Artificial: Compendium of Digital Art Volume 1 (Artificial Intelligence Draws Art) — Kindle edition by P, Shaxib, A, Bixjesh. Arts & Photography Kindle eBooks @ Amazon.com.