GithubHelp home page GithubHelp logo

marikats / map-filter-lab-python-data-science-alpha Goto Github PK

View Code? Open in Web Editor NEW

This project forked from learn-co-students/map-filter-lab-python-data-science-alpha

0.0 1.0 0.0 283 KB

Jupyter Notebook 57.23% Python 42.77%

map-filter-lab-python-data-science-alpha's Introduction

Map and Filter in Python Lab

Introduction

Let's continue to work with our Yelp Api.

from restaurants import yelp_restaurants
# dict_keys(['categories', 'coordinates', 'display_phone', 'distance', 
# 'id', 'image_url', 'is_closed', 'location', 'name', 'phone', 'price', 
# 'rating', 'review_count', 'transactions', 'url'])
restaurants = list(map(lambda restaurant: dict(name=restaurant['name'], 
                                           price=restaurant['price'], 
                                           is_closed=restaurant['is_closed'],
                                           review_count=restaurant['review_count'],
                                          ), yelp_restaurants))

We have a list of five restaurants from the Yelp Api. Let's take a look at the list.

restaurants
[{'is_closed': False,
  'name': 'Fork & Fig',
  'price': '$$',
  'review_count': 610},
 {'is_closed': False,
  'name': 'Salt And Board',
  'price': '$$',
  'review_count': 11},
 {'is_closed': False,
  'name': 'Frontier Restaurant',
  'price': '$',
  'review_count': 1373},
 {'is_closed': False,
  'name': 'Nexus Brewery',
  'price': '$$',
  'review_count': 680},
 {'is_closed': False,
  'name': "Devon's Pop Smoke",
  'price': '$$',
  'review_count': 54},
 {'is_closed': True,
  'name': 'Cocina Azul',
  'price': '$$',
  'review_count': 647},
 {'is_closed': False,
  'name': 'Philly Steaks',
  'price': '$$',
  'review_count': 25},
 {'is_closed': True,
  'name': 'Stripes Biscuit',
  'price': '$$',
  'review_count': 20}]

Using map

As you can see, it's a little tricky to see the names of all of the restaurants. Assign a variable names to the list of names of the functions. Use the map function to do so.

names = list(map(lambda restaurant: restaurant['name'] ,restaurants))
names
['Fork & Fig',
 'Salt And Board',
 'Frontier Restaurant',
 'Nexus Brewery',
 "Devon's Pop Smoke",
 'Cocina Azul',
 'Philly Steaks',
 'Stripes Biscuit']

Let's get a sense of how many reviews were written for each of the restaurants. Assign a variable review_counts to equal a list of the review_count for each restaurant.

review_counts = list(map(lambda restaurant: restaurant['review_count'] ,restaurants))
review_counts
[610, 11, 1373, 680, 54, 647, 25, 20]

Now add up the elements in the list, and assign the result to a variable named total_reviews.

total_reviews = sum(review_counts)
total_reviews
3420

It's a little tricky to work with the price in the format of dollars signs. So write a called format_restaurants that changes each restaurant to have the attribute 'price' point to the number of dollar signs. We'll get you started with the function, format_restaurant.

def format_restaurant(restaurant):
    if type(restaurant['price']) == str:
        restaurant['price'] = len(restaurant['price'])
    return restaurant
format_restaurant(restaurants[0])
{'is_closed': False, 'name': 'Fork & Fig', 'price': 2, 'review_count': 610}

Now write a function called format_restaurants, that returns a list of restaurants with each of them formatted with price pointing to the respective number.

def format_restaurants(restaurants):
    return list(map(format_restaurant,restaurants))
format_restaurants(restaurants)

Filter

Now let's search for restaurants based on specific criteria.

Write a function called open_restaurants that takes in a list of restaurants and only returns those that are open.

def open_restaurants(restaurants):
    return list(filter(lambda restaurant:not restaurant['is_closed'] ,restaurants))
open_restaurants(restaurants)

# [{'is_closed': False, 'name': 'Fork & Fig', 'price': 2, 'review_count': 610},
#  {'is_closed': False,
#   'name': 'Salt And Board',
#   'price': 2,
#   'review_count': 11},
#  {'is_closed': False,
#   'name': 'Frontier Restaurant',
#   'price': 1,
#   'review_count': 1373},
#  {'is_closed': False,
#   'name': 'Nexus Brewery',
#   'price': 2,
#   'review_count': 680},
#  {'is_closed': False,
#   'name': "Devon's Pop Smoke",
#   'price': 2,
#   'review_count': 54},
#  {'is_closed': False, 'name': 'Philly Steaks', 'price': 2, 'review_count': 25}]

Now write a function called cheapest_restaurants that returns restaurants those restaurants that have a price of 1, or '$'.

def cheapest_restaurants(restaurants):
    return list(filter(lambda restaurant: restaurant['price'] == 1 ,restaurants))
cheapest_restaurants(restaurants)

# [{'is_closed': False,
#   'name': 'Frontier Restaurant',
#   'price': 1,
#   'review_count': 1373}]
[{'is_closed': False,
  'name': 'Frontier Restaurant',
  'price': 1,
  'review_count': 1373}]

Summary

Great! Through this lab we saw how to pass arguments to functions, multiple arguments to functions, and how to implement functinons with default arguments.

map-filter-lab-python-data-science-alpha's People

Contributors

jeffkatzy avatar tkoar avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.