Giter VIP home page Giter VIP logo

dsc-lambda-functions-lab-dc-ds-100719's Introduction

Lambda Functions - Lab

Introduction

In this lab, you'll get some hands-on practice creating and using lambda functions.

Objectives

In this lab you will:

  • Create lambda functions to use as arguments of other functions
  • Use the .map() or .apply() method to apply a function to a pandas series or DataFrame

Lambda Functions

import pandas as pd
df = pd.read_csv('Yelp_Reviews.csv', index_col=0)
df.head(2)

Simple arithmetic

Use a lambda function to create a new column called 'stars_squared' by squaring the stars column.

# Your code here

Dates

Select the month from the date string using a lambda function.

# Your code here

What is the average number of words for a yelp review?

Do this with a single line of code!

# Your code here

Create a new column for the number of words in the review

# Your code here

Rewrite the following as a lambda function

Create a new column 'Review_Length' by applying this lambda function to the 'Review_num_words' column.

# Rewrite the following function as a lambda function
def rewrite_as_lambda(value):
    if len(value) < 50:
        return 'Short'
    elif len(value) < 80:
        return 'Medium'
    else:
        return 'Long'
# Hint: nest your if, else conditionals

df['Review_length'] = None

Level Up: Dates Advanced!

Print the first five rows of the 'date' column.

# Your code here

Overwrite the 'date' column by reordering the month and day from YYYY-MM-DD to DD-MM-YYYY. Try to do this using a lambda function.

# Your code here

Summary

Great! Hopefully, you're getting the hang of lambda functions now! It's important not to overuse them - it will often make more sense to define a function so that it's reusable elsewhere. But whenever you need to quickly apply some simple processing to a collection of data you have a new technique that will help you to do just that. It'll also be useful if you're reading someone else's code that happens to use lambdas.

dsc-lambda-functions-lab-dc-ds-100719's People

Contributors

budbernhard avatar fpolchow avatar loredirick avatar mathymitchell avatar mike-kane avatar peterbell avatar realandrewcohn avatar sumedh10 avatar

Watchers

 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.