Python for data

Lesson 7 of 15

Lesson ยท 15 min

Numbers that describe data

A few numbers for many values

A list of 15 trip times is hard to read. A few numbers describe it: the number of values, the smallest value, the largest value, and a value in the middle.

The mean

The mean is the average: the total divided by the number of values. The module statistics has a function for it.

import statistics

times = [10, 12, 14, 20]
print(statistics.mean(times))   # 14

The median

Sort the values. The median is the value in the middle. When the number of values is even, the median is the mean of the two values in the middle.

print(statistics.median([10, 12, 14, 20, 90]))   # 14
print(statistics.median([10, 12, 14, 20]))       # 13.0

Half of the values are below the median, and half are above it.

One strange value moves the mean

Add one very large value to the list.

times = [10, 12, 14, 20, 600]
print(statistics.mean(times))     # 131.2
print(statistics.median(times))   # 14

The mean is now 131.2, and no value in the list is near that number. The median is still 14. One strange value moves the mean a long way, and it moves the median very little. A value that is far from all the others is an outlier.

Calculate both numbers. When the mean and the median are far apart, look for outliers, and find out what they are. An outlier can be a mistake in the data. It can also be a real event that you must report.

The smallest and the largest value

min and max show the two ends of the data. Look at them before you trust a mean. A trip of 0 minutes or of 600 minutes tells you something about the data.

An empty list

statistics.mean([]) stops with an error, because an empty list has no mean. A function that describes data must decide what it returns for an empty list. Check the length first.

Your turn

main.py loads the 15 trips of trips.csv. Do the tasks in order.

Tasks

5 to do

  1. Task 1, not checked yet. Make a list minutes with the minutes of each trip. Make a variable mean_minutes with the mean of the list, rounded to 1 digit after the decimal point.

  2. Task 2, not checked yet. Make a variable median_minutes with the median of minutes.

  3. Task 3, not checked yet. The mean is almost 3 times the median, because one bike came back after 610 minutes. Make a list usual with the values of minutes that are 180 or less. Make a variable mean_usual with its mean, rounded to 1 digit.

  4. Task 4, not checked yet. Write a function describe(values). It returns a dictionary with the keys count, min, max, mean and median. The mean has 1 digit after the decimal point. For an empty list, it returns {"count": 0}.

  5. Task 5, not checked yet. Write a function outliers(values, factor). It returns the list of the values that are larger than factor times the median. For an empty list, it returns an empty list.

Task 1 of 5

Make a list minutes with the minutes of each trip. Make a variable mean_minutes with the mean of the list, rounded to 1 digit after the decimal point.

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.