Skip to content

learn-co-students/dsc-1-04-04-lambda-functions-lab-seattle-ds-career-040119

Repository files navigation

Lambda Functions - Lab

Introduction|

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

Objectives

You will be able to:

  • Understand what lambda functions are and why they are useful
  • Use lambda functions to transform data within lists and DataFrames

Lambda Functions

import pandas as pd
df = pd.read_csv('Yelp_Reviews.csv')
df.head(2)
<style> .dataframe thead tr:only-child th { text-align: right; }
.dataframe thead th {
    text-align: left;
}

.dataframe tbody tr th {
    vertical-align: top;
}
</style>
Unnamed: 0 business_id cool date funny review_id stars text useful user_id
0 1 pomGBqfbxcqPv14c3XH-ZQ 0 2012-11-13 0 dDl8zu1vWPdKGihJrwQbpw 5 I love this place! My fiance And I go here atl... 0 msQe1u7Z_XuqjGoqhB0J5g
1 2 jtQARsP6P-LbkyjbO1qNGg 1 2014-10-23 1 LZp4UX5zK3e-c5ZGSeo3kA 1 Terrible. Dry corn bread. Rib tips were all fa... 3 msQe1u7Z_XuqjGoqhB0J5g

Simple Arithmetic

Use a lambda function to create a new column called 'stars_squared' by squarring 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 lamda function. Create a new column 'Review_Length'

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
#Your code here

Level Up: Dates Adavanced!

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 elses code that happens to use lambdas.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published