Python for data

Lesson 6 of 15

Lesson ยท 18 min

Count and group

How many of each

"Which station is the busiest?" To answer this, you count the trips of each station. A dictionary holds one count for each station. The station is the key, and the count is the value.

counts = {}
for trip in trips:
    station = trip["start"]
    counts[station] = counts.get(station, 0) + 1

counts.get(station, 0) gives the count so far. It gives 0 when the station is not in the dictionary yet. The line then stores that count plus one.

Counter

People count so often that Python has a tool for it, in the module collections.

from collections import Counter

counts = Counter([trip["start"] for trip in trips])
print(counts["Harbor Gate"])   # the number of trips from Harbor Gate
print(counts.most_common(2))   # the 2 largest counts, the largest first

A Counter is a dictionary with extra methods. most_common(2) gives a list of pairs, for example [('Harbor Gate', 5), ('Town Hall', 3)]. Each pair is a tuple: the value, and then its count. So counts.most_common(1)[0][0] is the value with the largest count.

A total for each group

The same pattern adds a number in place of 1.

minutes = {}
for trip in trips:
    bike = trip["bike"]
    minutes[bike] = minutes.get(bike, 0) + trip["minutes"]

An average for each group

An average needs a total and a count for each group. Make the two dictionaries, and divide at the end. A for loop over a dictionary gives its keys.

bike_counts = Counter([trip["bike"] for trip in trips])

averages = {}
for bike in minutes:
    averages[bike] = round(minutes[bike] / bike_counts[bike], 1)

The rows of each group

Sometimes you need the rows of each group, and not only a number. Keep a list for each key.

groups = {}
for trip in trips:
    rider = trip["rider"]
    if rider not in groups:
        groups[rider] = []
    groups[rider].append(trip)

groups["member"] is then the list of all the trips of members.

Your turn

main.py loads the 18 trips of trips.csv. Do the tasks in order. The last two tasks ask for a function that works for each column.

Tasks

5 to do

  1. Task 1, not checked yet. Make a dictionary start_counts. Each key is a start station, and its value is the number of trips that start there.

  2. Task 2, not checked yet. Make a variable busiest with the name of the start station that has the most trips. Take the name from your counts.

  3. Task 3, not checked yet. Make a dictionary minutes_by_rider. Each key is a rider type, and its value is the total minutes of the trips of that type.

  4. Task 4, not checked yet. Write a function count_by(trips, column). It returns a dictionary with the number of trips for each value of that column.

  5. Task 5, not checked yet. Write a function average_by(trips, column). It returns a dictionary with the average minutes for each value of that column. Each average has 1 digit after the decimal point.

Task 1 of 5

Make a dictionary start_counts. Each key is a start station, and its value is the number of trips that start there.

All tasks

Ctrl+Enter runs your code. Ctrl+Shift+Enter checks it. Press Esc, then Tab, to leave the editor.

Output

Press Run to see your result

Your output shows here. Press Run to try your code.