""" Felix Muzny DS 2000 Lecture 12 October 18, 2024 Data file (all sept 2024 ebike trips starting at Forsyth) Three columns: - end station name - end station latitude (latitude is flatitude) - end station longitude (longitude is veritcal) Goals for today’s code: - read a csv into a list of lists - how many trips were to "Brigham Circle - Francis St at Huntington" - where did the BlueBikes riders go from NEU in September? """ import matplotlib.pyplot as plt # to help us read CSVs import csv FILENAME = "bluebikes_forsyth.csv" STATION_OF_INTEREST = "Brigham Circle - Francis St at Huntington Ave" FORSYTH_LAT = 42.339202 FORSYTH_LONG = -71.090511 STATION_COL = 0 LAT_COL = 1 LONG_COL = 2 def read_csv(filename): ''' Function: read_csv parameter: filename, a string Returns: 2d list of strings, the contents of the file ''' data = [] with open(filename, "r") as infile: csvfile = csv.reader(infile, delimiter=",") for row in csvfile: data.append(row) return data def get_station(data, station_name, col): """ Get all rows from the data where the station name :param data: list of lists of strings :param station_name: string station name to search for :param col: int column to look in for the station name :return: list of ints """ new_rows = [] for row in data: # secret python code: # row = next sublist in the data # row[col] will be the station name in this row if row[col] == station_name: new_rows.append(row) return new_rows def plot_locations(data, lat_col, long_col, begin_lat, begin_long): """ Make a scatter plot of all the end locations given by the lat and long columns in the data. Plot one point as a special point to differentiate it from the others :param data: list of lists of strings :param lat_col: int column with the latitude information :param long_col: int column with the longitude information :param begin_lat: int special point's latitude :param begin_long: int special point's longitude :return: nothing """ # longitudes xs = [] # latitudes ys = [] for row in data: xs.append(float(row[long_col])) ys.append(float(row[lat_col])) plt.plot(xs, ys, "*", color="blue", label="destinations") # plot the special point second so it appears second plt.plot(begin_long, begin_lat, "s", color="red", label="Forsyth at Huntington") plt.xlabel("longitude") plt.ylabel("latitude") plt.legend() plt.savefig("lec12_bluebikes.png") plt.show() def main(): print("lec 12 - bluebikes") data = read_csv(FILENAME) print("Number of rows:", len(data)) print("First row:", data[0]) # get the rows for the station we care about station_rows = get_station(data, STATION_OF_INTEREST, STATION_COL) print("Number of trips to", STATION_OF_INTEREST, ":", len(station_rows)) # if time, let's plot these! plot_locations(data, LAT_COL, LONG_COL, FORSYTH_LAT, FORSYTH_LONG) main()