Python for data

Lesson 4 of 15

Lesson ยท 16 min

Filter and pick

A module of your own

From this lesson on, main.py does not read the file itself. The lesson has a file named bikedata.py. It holds the function load_trips that you wrote in the last lesson. A Python file that other code imports is a module, the same as csv.

from bikedata import load_trips

trips = load_trips("trips.csv")

from bikedata import load_trips loads one name from the module. You then call load_trips without the name of the module in front of it. Select the tab bikedata.py to read the code.

Filter: keep some rows

Most questions about data start with the words "which rows". You know the loop that answers them.

long_trips = []
for trip in trips:
    if trip["minutes"] > 30:
        long_trips.append(trip)

Python has a short form for this loop. Its name is list comprehension.

long_trips = [trip for trip in trips if trip["minutes"] > 30]

Read it in three parts, and start in the middle:

  • for trip in trips takes each trip.
  • if trip["minutes"] > 30 keeps a trip only when the condition is true.
  • The first trip is the value that goes into the new list.

The list trips does not change. You get a new list.

Pick: keep one value of each row

The first part can be any value. Here it is one column.

starts = [trip["start"] for trip in trips]

You can filter and pick in one step.

member_minutes = [trip["minutes"] for trip in trips if trip["rider"] == "member"]

Join two conditions with and or or, as you do in an if statement.

Numbers from a list

Code Result
len(values) the number of items
sum(values) the total of the numbers
min(values) the smallest value
max(values) the largest value
round(7.4567, 1) 7.5, the number with 1 digit after the decimal point
print(sum(member_minutes) / len(member_minutes))   # the average

The length of an empty list is 0, and Python cannot divide by 0. When a list can be empty, check that first.

Each value one time

A set keeps each value one time. set(starts) drops the names that repeat. A set has no order. sorted takes the values and gives a new list in order.

stations = sorted(set(starts))

Your turn

main.py already loads the 16 trips of trips.csv. Do the tasks in order. Two tasks ask for a function. The checker calls each function with other trips, so use the parameters of the function, and not the 16 trips of the file.

Tasks

5 to do

  1. Task 1, not checked yet. Make a list long_trips with the trips that took more than 30 minutes. Use a list comprehension.

  2. Task 2, not checked yet. Make a variable electric_minutes with the total minutes of the trips on an electric bike.

  3. Task 3, not checked yet. Make a list stations with the name of each start station one time, in alphabetical order.

  4. Task 4, not checked yet. Write a function trips_from(trips, station). It returns the list of the trips that start at station.

  5. Task 5, not checked yet. Write a function share(trips, rider). It returns the percent of the trips that have this rider, with 1 digit after the decimal point. For an empty list, it returns 0.

Task 1 of 5

Make a list long_trips with the trips that took more than 30 minutes. Use a list comprehension.

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.