Topfolio
Career TracksMicro Free CoursesTask CoursesPracticeInterview PracticeWork ExperienceProjects
Career TrackBlog
Support
Dashboard
Career TracksMicro Free CoursesTask Courses
SQL & Python
Interview Prep
Work ExperienceProjectsResume Feedback
CertificatesMy OrdersRefer & Earn
Join Community

Navigation

Dashboard
Learn
Career TracksMicro Free CoursesTask Courses
Practice
SQL & PythonInterview Prep
Career
Work ExperienceProjectsResume Feedback
My Stuff
CertificatesMy OrdersRefer & Earn
Join Community
Topfolio

Learn data analytics online. Real skills, real projects, real community.

courses

  • Career Tracks
  • Work Experience
  • Best Data Analyst Course in India
  • SQL Fundamentals
  • SQL Advanced
  • Python for Data
  • Micro Free Courses

practice

  • SQL Fundamentals Interview Test
  • SQL Advanced Coding Test
  • All Practice Tests

resources

  • How to Become a Data Analyst
  • SQL Interview Questions
  • Data Analyst Salary Guide
  • Free Datasets for Practice
  • SQL vs NoSQL Guide
  • Data Analyst vs Data Engineer vs Data Scientist

employers

  • Enterprise Screening
  • AI Proctoring Demo
  • Volume Pricing
  • Employer Portal

company

  • About
  • Terms
© 2026 Topfolio. All rights reserved.Made with ❤️ for aspiring analysts
Topfolio
Career TracksMicro Free CoursesTask CoursesPracticeInterview PracticeWork ExperienceProjects
Career TrackBlog
Support
Dashboard
Career TracksMicro Free CoursesTask Courses
SQL & Python
Interview Prep
Work ExperienceProjectsResume Feedback
CertificatesMy OrdersRefer & Earn
Join Community

Navigation

Dashboard
Learn
Career TracksMicro Free CoursesTask Courses
Practice
SQL & PythonInterview Prep
Career
Work ExperienceProjectsResume Feedback
My Stuff
CertificatesMy OrdersRefer & Earn
Join Community

Cohort Retention Matrix in Pandas

advanced~15 min35 pts
📊 View Insights

Problem

You're given a small event log of user activity: each row is one (user, month) pair in which that user was active, along with the month they originally signed up. Build a cohort retention matrix: rows are signup cohort (month), columns are months-since-signup (0, 1, 2, ...), and values are the retention rate — the share of that cohort active in that month.

Steps:

  1. For each row, compute months_since_signup = (active_month − signup_month) in whole months.
  2. Compute each cohort's size (distinct users whose signup_month equals that cohort).
  3. For each (signup_month, months_since_signup), count distinct active users and divide by the cohort size to get the retention rate.
  4. Pivot into a matrix: index=signup_month, columns=months_since_signup, values=retention rate. Months with no data for a cohort should be left as NaN (not zero) — a cohort simply hasn't reached that age yet.

Reset the index so signup_month is a normal column, round all rate values to 4 decimals, and assign the DataFrame to result.

Product AnalyticsCohort AnalysisRetentionpandasPivot Table
📊
📊 View Question Telemetry & Insights
Failure rate, silent bug autopsy & optimal query construction
Inspect →

Master SQL & Data Analytics

Ready to take your skills to the next level? Enroll in our comprehensive Data Analyst Career Track to master SQL, Python, Excel, and Power BI.

Explore the 12-Week Data Analyst Career Track

Related questions

  • Select a Single Columnbeginner
  • Select Multiple Columnsbeginner
  • Filter Rows by Conditionbeginner
  • Filter with Multiple Conditionsbeginner
  • Sort DataFrame by Columnbeginner
  • Get Top N Rowsbeginner
  • Count Rows and Columnsbeginner
  • Find Unique Valuesbeginner
Python Editor

Run your code to see the result here.

Topfolio

Learn data analytics online. Real skills, real projects, real community.

courses

  • Career Tracks
  • Work Experience
  • Best Data Analyst Course in India
  • SQL Fundamentals
  • SQL Advanced
  • Python for Data
  • Micro Free Courses

practice

  • SQL Fundamentals Interview Test
  • SQL Advanced Coding Test
  • All Practice Tests

resources

  • How to Become a Data Analyst
  • SQL Interview Questions
  • Data Analyst Salary Guide
  • Free Datasets for Practice
  • SQL vs NoSQL Guide
  • Data Analyst vs Data Engineer vs Data Scientist

employers

  • Enterprise Screening
  • AI Proctoring Demo
  • Volume Pricing
  • Employer Portal

company

  • About
  • Terms
© 2026 Topfolio. All rights reserved.Made with ❤️ for aspiring analysts