Home 00 Labs 01 Nb 2 Html Tests 02 211 Web Scraper 03 Scooter Explorati... 04 Wordclouds 05 Amivi Meetup 08 Apis 09 Looking At Data 10 Satellltes Txt2speach

Don't Look! I'm changing!

URL Copied

BinderBinderBinderOpen Source Love svg3

NPM LicenseActiveGitHub last commit

GitHub starsGitHub watchersGitHub forksGitHub followers

The darker boxes of text are code.

If you click on one: hit shft + entr to execute the cell's code.

# This is a comment! You can tell by the way it's green and starts with a `#` 
 print('This line of code prints out the red text inside of it')
 
 # and here I store a variable called 'hi' and then print that too.
 my_test_variable = 'hi'
 print(my_test_variable)
This line of code prints out the red text inside of it hi

Follow along as we run the rest one by one?

This next line will install geopandas. It is the only library not pre-provided by COLABS

Pip is a tool colabs provides to install python libraries onto the virtual machine they provide us.

We use it's command line API here:

You may have seen a lot stuff it output...

Feel free to hit the X button in the left of the cell (when hovering over) to delete it.

Now the virtual machine has our needed library.

With that done, formally 'import' the library into our python runtime enviornment so we may use it.

import geopandas

This next part is a little bit hard.

Hit this link. Find a statistic you'd like to visualize. Copy and paste its shortname into the code cell below.

shortname = 'libcard'

Lets see our final query?

# Create the url we will use to query the data from the ESRI api endpoint.
 baseurl = "https://services1.arcgis.com/mVFRs7NF4iFitgbY/ArcGIS/rest/services/"
 slug = "/FeatureServer/0/query?where=1%3D1&outFields=*&returnGeometry=true&f=pgeojson"
 url = baseurl+shortname+slug 

And were all set!

For a simple pull of our data, just run the next cell!

# Use the geopandas library to read it in and immediately set it's index and drop an undesired column.
 gdf = geopandas.read_file(url).set_index('CSA2010').drop(axis='1', columns=['OBJECTID','Shape__Area','Shape__Length'])

Lets see it?

Right of the bat, we can use two different libraries to get dramatically different results:

# Change the number here to get a different number of rows
 # gdf.head(2)
 gdf.tail(4) 
 # this works too! just uncomment it
libcard11libcard12libcard13libcard14libcard15libcard16libcard17libcard18libcard19geometry
CSA2010
The Waverlies358.957823345.672643270.346962447.826648504.449890458.274216403.714691405.004514400.490133POLYGON ((-76.59550 39.33416, -76.59548 39.334...
Upton/Druid Heights293.656933285.341327253.142526426.513247465.673951382.517888315.993038316.863276310.674918POLYGON ((-76.63401 39.31045, -76.62509 39.303...
Washington Village/Pigtown318.916954301.835363271.669998422.496820446.302017370.343449319.280393329.820098331.092132POLYGON ((-76.62096 39.27466, -76.62110 39.274...
Westport/Mount Winans/Lakeland155.499368149.318725133.445709246.382919267.734232229.526619181.345695185.981177176.288805POLYGON ((-76.62627 39.26790, -76.62631 39.267...
gdf.plot()
Image Alt Text
gdf.plot(column='libcard11')
Image Alt Text

Use the `plot function to see a map!

Geopandas provides a convenient webpage to help you get really pretty results in not time!

import matplotlib.pyplot as plt
 fig, ax = plt.subplots(1, 1)
 gdf.plot(column='libcard11', ax=ax, legend=True, 
            legend_kwds={'label': "Library Card Access 2011", 'orientation': "horizontal"})
Image Alt Text

Awesome!

If you want to save the data you may use this csv library.

import csv
 gdf.drop(axis='1', columns=['geometry']).to_csv(shortname+'.csv', quoting=csv.QUOTE_ALL) 

A full, unedited list of our available endpoints may be found here

You can also click this link, find the service and or layer you want to query; and there will be a little 'query' button in the bottom left which will take you to an advanced GUI to help you generate custom queries.

example: https://services1.arcgis.com/mVFRs7NF4iFitgbY/ArcGIS/rest/services/Aastud/FeatureServer/query

NOW!

If we want to change our visualization of the data.

We can use the underlying library used by geopandas itself: matplotlib, and seaborn.

Geojson data does not play nicely with these kind of maps. so we will drop it "for now."

df = gdf.drop(axis='1', columns=['geometry'])
import numpy as np 
 from pandas import DataFrame
 import seaborn as sns
 %matplotlib inline
 # We can change the size of our images like this:
 plt.figure(figsize=(20,20))
 
 # And heatmaps are as simple as this:
 sns.heatmap(df, annot=True)
Image Alt Text
df.head(2).plot.line()
Image Alt Text

But these look a bit aweful.

A full list of visualizations you may use with the seaborn and matplotlib libraries can be found on each of their corresponding websites.

What will you do?

We may use the remainder of our time today to explore some of these libraries, API's and most importantly

UNDERSTANDING THEIR DOCUMENTATION USING THE LINKS ABOVE.

The rest of this doc is a playground: