Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Challenge: User Segmentation Script | Analyzing User Behavior
Python for Growth Hackers

bookChallenge: User Segmentation Script

In growth hacking, understanding how users engage with your product is crucial for tailoring marketing strategies. Segmenting users based on their activity helps you identify who is highly engaged, who might need a nudge, and who has stopped interacting altogether. You will now create a script to automate this segmentation process using Python and pandas. The goal is to write a function that categorizes users into three segments based on their activity counts: active for those with 10 or more activities, dormant for those with 1 to 9, and churned for those with zero activity. This approach lets you quickly spot trends and target users with the right messaging.

12345678910111213141516171819202122232425
import pandas as pd def segment_users(): # Hardcoded user activity data data = { "user_id": ["u1", "u2", "u3", "u4", "u5", "u6"], "activity_count": [12, 0, 7, 3, 0, 15] } df = pd.DataFrame(data) # Function to assign segment def assign_segment(activity): if activity >= 10: return "active" elif activity >= 1: return "dormant" else: return "churned" df["segment"] = df["activity_count"].apply(assign_segment) return df[["user_id", "segment"]] # Run the function and display the result segmented_df = segment_users() print(segmented_df)
copy

This script uses a hardcoded DataFrame to simulate real user data. Each user is assigned a segment based on their activity count. The segmentation logic is handled by the assign_segment function, which is applied to each user's activity value. The resulting DataFrame gives you a clear view of which users are "active", "dormant", or "churned", making it easier to plan your next growth move.

Завдання

Swipe to start coding

Write a function called segment_users that:

  • Creates a pandas DataFrame with two columns: "user_id" and "activity_count".
  • Hardcodes at least five users with varying activity counts (including at least one with 0, one with 1–9, and one with 10 or more).
  • Assigns each user to a segment:
    • "active" if activity_count is 10 or more.
    • "dormant" if activity_count is between 1 and 9 (inclusive).
    • "churned" if activity_count is 0.
  • Returns a new DataFrame with columns "user_id" and "segment".

Your function should not take any arguments.

Test your function by calling it and printing the result.

Рішення

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 5
single

single

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Suggested prompts:

Can you explain how I can use this segmentation in a real-world scenario?

How can I modify the script to use my own user data instead of the hardcoded values?

What are some next steps I can take after segmenting my users?

close

bookChallenge: User Segmentation Script

Свайпніть щоб показати меню

In growth hacking, understanding how users engage with your product is crucial for tailoring marketing strategies. Segmenting users based on their activity helps you identify who is highly engaged, who might need a nudge, and who has stopped interacting altogether. You will now create a script to automate this segmentation process using Python and pandas. The goal is to write a function that categorizes users into three segments based on their activity counts: active for those with 10 or more activities, dormant for those with 1 to 9, and churned for those with zero activity. This approach lets you quickly spot trends and target users with the right messaging.

12345678910111213141516171819202122232425
import pandas as pd def segment_users(): # Hardcoded user activity data data = { "user_id": ["u1", "u2", "u3", "u4", "u5", "u6"], "activity_count": [12, 0, 7, 3, 0, 15] } df = pd.DataFrame(data) # Function to assign segment def assign_segment(activity): if activity >= 10: return "active" elif activity >= 1: return "dormant" else: return "churned" df["segment"] = df["activity_count"].apply(assign_segment) return df[["user_id", "segment"]] # Run the function and display the result segmented_df = segment_users() print(segmented_df)
copy

This script uses a hardcoded DataFrame to simulate real user data. Each user is assigned a segment based on their activity count. The segmentation logic is handled by the assign_segment function, which is applied to each user's activity value. The resulting DataFrame gives you a clear view of which users are "active", "dormant", or "churned", making it easier to plan your next growth move.

Завдання

Swipe to start coding

Write a function called segment_users that:

  • Creates a pandas DataFrame with two columns: "user_id" and "activity_count".
  • Hardcodes at least five users with varying activity counts (including at least one with 0, one with 1–9, and one with 10 or more).
  • Assigns each user to a segment:
    • "active" if activity_count is 10 or more.
    • "dormant" if activity_count is between 1 and 9 (inclusive).
    • "churned" if activity_count is 0.
  • Returns a new DataFrame with columns "user_id" and "segment".

Your function should not take any arguments.

Test your function by calling it and printing the result.

Рішення

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 5
single

single

some-alt