Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Challenge: Retention Cohort Analyzer | Analyzing User Behavior
Python for Growth Hackers
セクション 2.  7
single

single

bookChallenge: Retention Cohort Analyzer

メニューを表示するにはスワイプしてください

To deepen your understanding of cohort analysis, you will write a Python script that simulates a simple retention cohort analyzer. Cohort analysis is a powerful tool for growth hackers, as it helps you track user retention over time by grouping users based on shared characteristics—in this case, their signup month. You will use a hardcoded pandas DataFrame to represent user data, including user IDs, their signup months, and retention status (where 1 means retained and 0 means not retained). Your task is to calculate the retention rate for each signup month and display the results, giving you practical experience with both pandas and user retention analytics.

Begin by importing the pandas library and creating a DataFrame with the necessary columns. The DataFrame will contain three columns: user_id, signup_month, and retained. Each row represents a user, their signup month, and whether they were retained.

123456789101112131415161718192021
import pandas as pd # Hardcoded user data for cohort analysis data = { "user_id": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], "signup_month": ["2024-01", "2024-01", "2024-01", "2024-02", "2024-02", "2024-02", "2024-03", "2024-03", "2024-03", "2024-03"], "retained": [1, 0, 1, 1, 0, 1, 0, 1, 1, 0] } df = pd.DataFrame(data) # Calculate retention rate for each signup month cohort = df.groupby("signup_month")["retained"].mean().reset_index() cohort["retention_rate"] = (cohort["retained"] * 100).round(2) # Print the results for _, row in cohort.iterrows(): print( f"Signup Month: {row['signup_month']} - Retention Rate: {row['retention_rate']}%" )
copy

This script groups users by their signup_month and calculates the average of the retained column for each group, which represents the retention rate. The retention rate is then multiplied by 100 and rounded to two decimal places for readability. Finally, the script prints the retention rate for each signup month, allowing you to see which cohorts are performing better in terms of user retention. By analyzing these results, you can identify trends and make data-driven decisions to improve user engagement and retention strategies.

タスク

スワイプしてコーディングを開始

Write a Python script that:

  • Imports the pandas library.
  • Creates a DataFrame with the following columns: user_id, signup_month, and retained.
  • The DataFrame should have at least 8 rows, with at least two unique values for signup_month.
  • Calculates the retention rate (mean of retained) for each signup month.
  • Prints each signup month and its retention rate as a percentage, rounded to two decimal places.

解答

Switch to desktop実践的な練習のためにデスクトップに切り替える下記のオプションのいずれかを利用して、現在の場所から続行する
すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 2.  7
single

single

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

some-alt