Final Exam_ Sample solutions _ Final Exam _ FA20_ Computing for Data Analysis
At a minimum, you will need the following modules in this problem. They include a new one we did not cover called geopandas. While it may be new to you, if you have mastered pandas, then you know almost everything you need to use geopandas. Anything else you need will be given to you as part of this problem, so don't be intimidated! In [1]: * Python version: 3.7.5 (default, Dec 18 2019, 06:24:58) [GCC 5.5.0 ] * geopandas version: 0.6.2 Run the next code cell, which will load some tools needed by the test cells. In [2]: Part 1: Neighborhood ratings The neighborhood rating data is stored in a special extension of a pandas DataFrame called a GeoDataFrame. Let's load the data into a variable named neighborhood_ratings and have a peek at the first few rows: In [3]: Opening geopandas data file, './resource/asnlib/publicdata/fullD json' ... class 'taframe.GeoDataFrame' Out[3]: state city name holc_id holc_grade area_description_data ge 0 AL Birmingham Mountain Brook Estates and Country Club Garden... A1 A {'5': 'Both sales and rental prices in 1929 we... MU (((- 33. -86 1 AL Birmingham Redmont Park, Rockridge Park, Warwick Manor, a... A2 A {'5': 'Both sales and rental prices in 1929 we... MU (((- 33. -86 2 AL Birmingham Colonial Hills, Pine Crest (outside city limits) A3 A {'5': 'Generally speaking, houses are not buil... MU (((- 33. -86 3 AL Birmingham Grove Park, Hollywood, Mayfair, and Edgewood s... B1 B {'5': 'Both sales and rental prices in 1929 we... MU (((- 33. -86 4 AL Birmingham Best section of Woodlawn Highlands B10 B {'5': 'Both sales and rental prices in 1929 we... MU (((- 33. -86 Each row is a neighborhood. Its location is given by name, city, and a two-letter state abbreviation code (the name, city, and state columns, respectively). The rating assigned to a neighborhood is a letter, 'A', 'B', 'C', or 'D', given by the holc_grade column. In addition, there is special column called geometry. It contains a geographic outline of the boundaries of this neighborhood. Let's take a look at row 4 (last row shown above): In [4]: * Type of `g4_example`: class 'polygon.MultiPolygo n' * Contents of `g4_example`: MULTIPOLYGON (((-86. 33., -86.74915 6 33., -86.99999 33., -86. 33., -86. 33., -86. 33., -86.00001 33., -86. 33., -86. 33., -86. 33., -86. 3 3., -86. 33.))) * A quick visual preview: The output indicates that this boundary is stored a special object type called a MultiPolygon. It is usually a single connected polygon, but may also be the union of multiple such polygons. The coordinates of the multipolygon's corners are floating-point values, and correspond to longitude and latitude values. But for this notebook, the exact format won't be important. Simply treat the shapes as being specified in some way via a collection of two-dimensional (x, y) (x,y) coordinates measured in arbitrary units. Lastly, observe that calling display() on a MultiPolygon renders a small picture of it. Exercise 0: Filtering ratings (2 points) Complete the function, def filter_ratings(ratings, city_st, targets=None): ... so that it filters ratings data by its city and state name, along with a set of targeted letter grades. In particular, the inputs are: ratings: A geopandas GeoDataFrame similar to the neighborhood_ratings example above. city_st: The name of a city and two-letter state abbreviation as a string, e.g., city_st = 'Atlanta, GA' to request only rows for Atlanta, Georgia. targets: A Python set containing zero or more ratings, e.g., targets = {'A', 'C'} to request only rows having either an 'A' grade or a 'C' grade. The function should return a copy of the input GeoDataFrame that has the same columns as ratings but only rows that match both the desired city_st value and any one of the target ratings.
Document information
- Uploaded on
- December 14, 2022
- Number of pages
- 51
- Written in
- 2022/2023
- Type
- Exam (elaborations)
- Contains
- Questions & answers