# CrossCompute
'''https://nbviewer.jupyter.org/github/vincentropy/python_cartography_tutorial/blob/master/part1_basic_folium_maps.ipynb'''
import pandas as pd
import folium
folium_map = folium.Map(location=[40.738, -73.98],
zoom_start=13,
tiles="CartoDB dark_matter")
folium.CircleMarker(location=[40.738, -73.98],fill=True).add_to(folium_map)
folium_map
bike_data = pd.read_csv('201306-citibike-tripdata - Copy.csv', na_values = 'n/a')
bike_data["starttime"] = pd.to_datetime(bike_data["starttime"])
bike_data["stoptime"] = pd.to_datetime(bike_data["stoptime"])
bike_data["hour"] = bike_data["starttime"].map(lambda x: x.hour)
bike_data.head()
def get_trip_counts_by_hour(selected_hour):
# make a DataFrame with locations for each bike station
locations = bike_data.groupby("start station id").first()
locations = locations.loc[:, ["start station latitude",
"start station longitude",
"start station name"]]
#select one time of day
subset = bike_data[bike_data["hour"]==selected_hour]
# count trips for each destination
departure_counts = subset.groupby("start station id").count()
departure_counts = departure_counts.iloc[:,[0]]
departure_counts.columns= ["departure Count"]
# count trips for each origin
arrival_counts = subset.groupby("end station id").count().iloc[:,[0]]
arrival_counts.columns= ["arrival count"]
#join departure counts, arrival counts, and locations
trip_counts = departure_counts.join(locations).join(arrival_counts)
return trip_counts
# print a sample to check our code works
get_trip_counts_by_hour(6).head()
def plot_station_counts(trip_counts):
# generate a new map
folium_map = folium.Map(location=[40.738, -73.98],
zoom_start=13,
tiles="CartoDB dark_matter",
width='50%')
# for each row in the data, add a cicle marker
for index, row in trip_counts.iterrows():
# calculate net departures
net_departures = (row["departure count"]-row["arrival count"])
# generate the popup message that is shown on click.
popup_text = "{}<br> total departures: {}<br> total arrivals: {}<br> net departures: {}"
popup_text = popup_text.format(row["start station name"],
row["arrival count"],
row["departure count"],
net_departures)
# radius of circles
radius = net_departures/20
# choose the color of the marker
if net_departures>0:
# color="#FFCE00" # orange
# color="#007849" # green
color="#E37222" # tangerine
else:
# color="#0375B4" # blue
# color="#FFCE00" # yellow
color="#0A8A9F" # teal
# add marker to the map
folium.CircleMarker(location=(row["start station latitude"],
row["start station longitude"]),
radius=radius,
color=color,
popup=popup_text,
fill=True).add_to(folium_map)
return folium_map
# plot net departures at 9AM
trip_counts = get_trip_counts_by_hour(9)
plot_station_counts(trip_counts)
# plot net departures at 6PM
trip_counts = get_trip_counts_by_hour(18)
folium_map = plot_station_counts(trip_counts)
folium_map
folium_map.save("part_1.html")