Skip to content

Lesson 25 ยท Projects and Reference

Project: Explore Bike Rentals with SQL and Python

Explore an invented bike rental dataset with SQL and Python: check quality, study trip durations, compare rider types and test whether the mix explains it.

This project practices EDA across two tools. A city bike service asks: "Do casual riders take longer trips than members, and is that also true within weekdays and within weekends?" The data is invented and tiny, so you can check results by hand.

Step 1: State the plan

  • Grain: one row per trip.
  • Metric: median trip duration in minutes, plus the share of trips over 30 minutes.
  • Groups: rider type (member or casual) and day type.
  • Checks: impossible durations, missing values, duplicate trip IDs.
  • Expectation: casual riders ride longer, especially on weekends.

Step 2: Quality checks in SQL

SQL Profile the trips table

Press Run (or Ctrl + Enter) to see the result here.

You find one repeated ID, one missing rider type, one negative duration and one trip of exactly 1,440 minutes (24 hours). A value exactly at a round limit suggests an automatic cutoff when a bike was not returned properly. For this project, assume that the service confirms this.

Step 3: Clean in a view

Keep the raw table unchanged. A view or a cleaning function applies the rules every time.

SQL Clean rules and grouped summary

Press Run (or Ctrl + Enter) to see the result here.

DISTINCT is acceptable here because you can see that the repeated rows are identical in all four columns. SQLite has traditionally had no built-in median function, so you compute medians in Python in the next step.

Step 4: Medians and rider mix in Python

Python Overall and stratified comparison

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Compare the overall gap with the gaps inside each day type. Casual riders have longer medians in both day types, but the overall gap is larger than either inside gap. The difference in mix helps explain this: most casual trips are on weekends, when trips are longer for both groups. You met this idea in confounders and Simpson's paradox.

Step 5: Visualize

Python Strip plot of every trip

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With so few points, show all of them. A box plot of two values would suggest more knowledge than you have.

Step 6: Report with honest limits

Your report should say:

  • Raw rows, cleaned rows and each exclusion rule.
  • Medians by rider and day type, with counts.
  • That the overall gap partly reflects different weekend shares.
  • That two of the groups have only two trips each, so their medians are very unstable.
  • That the data cannot show why durations differ. Trip purpose and route were not recorded.
  • A next step: get several months of trips and repeat the same planned comparisons.

Extensions

  1. Add station and hour columns, then explore when each group rides.
  2. Check how including the 1,440-minute trip changes the member median and mean.
  3. Bootstrap the casual weekend median. Why does a small sample limit what you learn? See sanity checks and uncertainty.

Real trip data can reveal where individuals live and travel. Use anonymized or aggregated data where possible, remember that removing names may not be enough, and follow your organization's privacy rules.

Cross-check the two tools

Before you write conclusions, confirm that SQL and Python agree. The counts per rider and day type in the SQL view should match the lengths of the Python lists: six weekday member trips, two weekend member trips, two weekday casual trips and five weekend casual trips. If they differed, you would know that the cleaning rules in one tool did not match the other.

In this project the Python lists were typed by hand to keep each block independent. In your own work, load the cleaned rows from the database into Python, so that the rules exist in only one place and cannot drift apart.

A model conclusion

"In this small invented sample, casual riders had longer median trips than members on both weekdays and weekends. The overall gap was larger partly because casual riders made more of their trips on weekends. Two groups are too small to support firm numbers." Notice that it answers the question, explains the mix and states the main weakness without claiming a reason.

Decide what changes your answer

Before you finish, test how fragile the result is. Add one casual weekday trip of 8 minutes and recompute that group's median. With only two earlier values, a single new trip moves it noticeably. Then make the same change to the larger member weekday group and compare the effect.

Also ask whether the duration limit is reasonable. A trip lasting several hours might be a real day tour or a bike left undocked. The data alone cannot decide. Write down the limit you used, why you chose it and how many trips it removed, so that the service team can correct you if their rules differ.

Before you finish

Check that the numbers reconcile. The raw table has 19 rows. One is an exact repeat, one has no rider type, one has a negative duration and one hits the 24-hour limit. That leaves 15 cleaned trips, which equals the total of your four groups. If your own cleaning produces a different count, find the rows that differ before you compare any medians. Include this short row count in your report, because it lets a reader verify your exclusions at a glance.

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. Why keep the raw table and clean through a view or function?

    You can review and revise the rules at any time.
  2. 2. Why is the overall casual-member gap larger than the gap within each day type?

    Different mixes across day types add to the overall difference.
  3. 3. What is the main caution about the groups with two trips?

    Two observations tell you very little about the typical trip.

Summary

  • Write down grain, metric, groups, checks and expectations first.
  • Use SQL to profile and clean; use Python where it is more convenient.
  • Compare overall and within day type to separate mix effects.
  • Show every point when groups are tiny.
  • Report exclusions, uncertainty, unanswered "why" questions and next steps.

Next: EDA Glossary and Cheat Sheet

Your progress is saved in this browser. No account needed.