Home 00 Datalabs 01 Scooter Explorati... 02 211 Web Scraper 03 Nbdev Create Clea... 04 Wordclouds 05 Amivi Meetup 06 Courts Mechanical... 07 Nb 2 Html Tests 08 Apis
09 Looking At Data

Don't Look! I'm changing!

URL Copied

Musical loops

The darker boxes of text are code.

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

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:

Requirement already satisfied: geopandas in /usr/local/lib/python3.7/dist-packages (0.9.0) Requirement already satisfied: shapely>=1.6 in /usr/local/lib/python3.7/dist-packages (from geopandas) (1.7.1) Requirement already satisfied: pyproj>=2.2.0 in /usr/local/lib/python3.7/dist-packages (from geopandas) (3.2.1) Requirement already satisfied: fiona>=1.8 in /usr/local/lib/python3.7/dist-packages (from geopandas) (1.8.20) Requirement already satisfied: pandas>=0.24.0 in /usr/local/lib/python3.7/dist-packages (from geopandas) (1.1.5) Requirement already satisfied: setuptools in /usr/local/lib/python3.7/dist-packages (from fiona>=1.8->geopandas) (57.4.0) Requirement already satisfied: six>=1.7 in /usr/local/lib/python3.7/dist-packages (from fiona>=1.8->geopandas) (1.15.0) Requirement already satisfied: certifi in /usr/local/lib/python3.7/dist-packages (from fiona>=1.8->geopandas) (2021.5.30) Requirement already satisfied: attrs>=17 in /usr/local/lib/python3.7/dist-packages (from fiona>=1.8->geopandas) (21.2.0) Requirement already satisfied: click-plugins>=1.0 in /usr/local/lib/python3.7/dist-packages (from fiona>=1.8->geopandas) (1.1.1) Requirement already satisfied: click>=4.0 in /usr/local/lib/python3.7/dist-packages (from fiona>=1.8->geopandas) (7.1.2) Requirement already satisfied: cligj>=0.5 in /usr/local/lib/python3.7/dist-packages (from fiona>=1.8->geopandas) (0.7.2) Requirement already satisfied: munch in /usr/local/lib/python3.7/dist-packages (from fiona>=1.8->geopandas) (2.5.0) Requirement already satisfied: numpy>=1.15.4 in /usr/local/lib/python3.7/dist-packages (from pandas>=0.24.0->geopandas) (1.19.5) Requirement already satisfied: python-dateutil>=2.7.3 in /usr/local/lib/python3.7/dist-packages (from pandas>=0.24.0->geopandas) (2.8.2) Requirement already satisfied: pytz>=2017.2 in /usr/local/lib/python3.7/dist-packages (from pandas>=0.24.0->geopandas) (2018.9)

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.

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.

Lets see our final query?

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 https://services1.arcgis.com/mVFRs7NF4iFitgbY/ArcGIS/rest/services/libcard/FeatureServer/0/query?where=1%3D1&outFields=*&returnGeometry=true&f=pgeojson

And were all set!

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

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:

# 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...
Image Alt TextImage 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!

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.

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."

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 TextImage 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: