Welcome, this is the unit where you will go from programming to analyzing data to really working like a Data Scientist. Pandas is at the center of a lot of data science. The important thing about the pandas library is an object called a DataFrame. You will work with a lot of DataFrames during this course, but even more in your career as a Data Scientist.
You can think of a DataFrame as a kind of table, similar to an Excel spreadsheet, but much more powerful. We’ll save the rest of the introduction for the lessons, but you should know that the more you learn about pandas, the easier your work as a Data Scientist will be.
What will you learn ?
In this unit, you will start building proficiency in the pandas library, which is essential for working with data in Python.
After this course, you will be able to:
- Load, modify, and analyze a DataFrame
- Transform a Series of data
- Leverage lambda expressions to write custom programs to manipulate your DataFrames
Why is this important?
Pandas is at the center of a lot of the work Data Scientists do, and will be an invaluable tool in your skillset as you work with more data. Pandas lets you work with data in a way that nothing else can, and is designed to do all the things a Data Scientist does.
Perform SQL-like functions on multiple DataFrames
Lambda Functions
Learn how to define a Python function in one line!
A function is an object that is able to accept some sort of input, possibly modify it, and return some sort of output. In Python, a lambda function is a one-line shorthand for function. A simple lambda function might look like this:
add_two = lambda my_input: my_input + 2
So this code:
print(add_two(3))
print(add_two(100))
print(add_two(-2))
would print:
Let’s break this syntax down:
- The function is stored in a variable called
add_two
lambda
declares that this is a lambda function (if you are familiar with normal Python functions, this is similar to how we usedef
to declare a function)my_input
is what we call the input we are passing intoadd_two
- We are returning
my_input
plus 2 (with normal Python functions, we use the keywordreturn
)
Let’s write a lambda function that checks if a string is a substring of the string “This is the master string”
.
is_substring = lambda my_string: my_string in "This is the master string"
So, the code:
print(is_substring('I'))
print(is_substring('am'))
print(is_substring('the'))
print(is_substring('master'))
would print:
We might want a function that will perform differently based on different inputs. Let’s say that we have a function check_if_A_grade
that outputs 'Got an A!'
if a grade is at least 90, and otherwise says you 'Did not get an A…'
. So, the code:
print(check_if_A_grade(91))
print(check_if_A_grade(70))
print(check_if_A_grade(20))
would print:
>>> 'Got an A!'
>>> 'Did not get an A...'
>>> 'Did not get an A...'
We can do this using an if statement in our lambda function, with syntax that looks like:
<WHAT TO RETURN IF STATEMENT IS TRUE> if <IF STATEMENT> else <WHAT TO RETURN IF STATEMENT IS FALSE>
So this is what our check_if_A_grade
function might look like:
check_if_A_grade = lambda grade: 'Got an A!' if grade >= 90 else 'Did not get an A...'
This is what this line of code does:
- Declare lambda function with an input called
grade
(lambda grade:
) - Return
'Got an A!'
if this statement is true:grade >= 90
- Otherwise, return
'Did not get an A...'
if this statement is not true:grade >= 90
Lambda functions only work if we’re just doing a one line command. If we wanted to write something longer, we’d need a more complex function. Lambda functions are great when you need to use a function once. Because you aren’t defining a function, the reusability aspect functions is not present with lambda functions. By saving the work of defining a function, a lambda function allows us to efficiently run an expression and produce an output for a specific task, such as defining a column in a table, or populating information in a dictionary.
Now you can make simple Python functions in one line!
LAMBDA FUNCTION CODE CHALLENGE
Contains A
Welcome! Let’s dive into lambda functions by exploring the in
keyword!
This lesson will help you review lambda functions by providing some challenge exercises. If you need a refresher on the syntax of lambda functions, review this article on lambda functions.
In Python, you can check if a string contains a substring by using the keyword in
. For example, the line:
return "I" in "Team"
would return False
, as there is no "I"
in "Team"
. However:
return "I" in "I love Python"
returns True
, because there is an "I"
in "I love Python"
.
Remember that to make a lambda function you can use the syntax:
lambda my_input: <my_input modified somehow>
For example, a lambda that would return my_input
plus 1
would look like:
plus_one = lambda my_input: my_input+1
1.Create a lambda function named contains_a
that takes an input word
and returns True
if the input contains the letter 'a'
. Otherwise, return False
.
#Write your lambda function here
contains_a = lambda word : True if 'a' in word else False
print(contains_a("banana"))
print(contains_a("apple"))
print(contains_a("cherry"))
Long String
To find the number of characters in a string, we use len
. This block of code:
print(len("Hello"))
print(len("world!"))
print(len("Hello, world!"))
would print out:
5
6
13
1.Create a lambda function named long_string
that takes an input str
and returns True
if the string has over 12 characters in it. Otherwise, return False
.
#Write your lambda function here
long_string = lambda str: True if len(str) > 12 else False
print(long_string("short"))
print(long_string("photosynthesis"))
Ends With A
You can get a character of a string string_name
by using the syntax string_name[index]
, where index
is the place of character you want to get, starting at 0. The last character in the string is string_name[-1]
.
my_string = "Whoa! A seesaw"
print(my_string[0])
print(my_string[2])
print(my_string[-1])
would print
"W"
"o"
"w"
1.Create a lambda function named ends_in_a
that takes an input str
and returns True
if the last character in the string is an a
. Otherwise, return False
.
#Write your lambda function here
ends_in_a = lambda str: str[-1] == 'a'
print(ends_in_a("data"))
print(ends_in_a("aardvark"))
Double Or Zero
Python makes math easy. To multiply, you can use *
:
>>> 4*2
8
>>> 2*3
6
>>> 0*10
0
>>> 20*10
200
As a reminder, to return different output depending on different input, we can use if
and else
inside our lambda function:
add_or_subtract = lambda input_number: input_number - 1 if input_number >= 0 else input_number + 1
This function add_or_substract will return your input minus 1 if your input is positive or 0,and othersize will return your input plus 1.
Here are some examples of how it would work:
>>> add_or_subtract(0)
-1
>>> add_or_subtract(8)
7
>>> add_or_subtract(-4)
-3
1.Create a lambda function named double_or_zero
that takes an integer named num
. If num
is greater than 10
, return double num
. Otherwise, return 0
.
#Write your lambda function here
double_or_zero = lambda num: num * 2 if num > 10 else 0
print(double_or_zero(15))
print(double_or_zero(5))
Even/Odd
In Python,%,or the modulo operator ,returns the remainder after division.
>>> 4%2 #This divides evenly
0
>>> 7%3 #7/3 has a remainder of 1
1
>>> 27%10
7
>>> 30%10
0
You can use % 2
to determine if a number is even or odd. If it is even, there should be no remainder (an output of 0
). If it is odd, there should be a remainder of 1
:
>>> 4%2
0
>>> 7%2
1
>>> 9%2
1
>>> 0%2
0
1.Create a lambda function named even_or_odd
that takes an integer named num
. If num
is even, return "even"
. If num
is odd, return "odd"
.
#Write your lambda function here
even_or_odd = lambda num: "even" if num % 2 == 0 else "odd"
print(even_or_odd(10))
print(even_or_odd(5))
Mutiple of Three
In general,using %n will tell you if an integer is a multiple of n.If the result is 0, the integer is a multiple of n (since there is no remainder in the division):
>>> 4%4 #4 is a multiple of 4
0
>>> 12%5 #12 is not a multiple of 5
2
>>> 9%2 #9 is not a multiple of 2
1
>>> 100%10 #100 is a multiple of 10
0
1.Create a lambda function named multiple_of_three
that takes an integer named num
. If num
is a multiple of three, return "multiple of three"
. Otherwise, return "not a multiple"
.
#Write your lambda function here
multiple_of_three = lambda num: "multiple of three" if num%3 == 0 else "not a multiple"
print(multiple_of_three(9))
print(multiple_of_three(10))
Movie Rating
Comparisons can be done using:
<
: less than<=
: less than or equal to>
: greater than>=
: greater than or equal to==
: equal to!=
: not equal to
These statements return either True or False:
>>> 4 < 4
False
>>> 4 <= 4
True
>>> 9 > 2
True
>>> 9 >= 2
True
>>> 1 > 2
False
1.Create a lambda function named rate_movie
that takes a number named rating
. If rating
is greater than 8.5
, return "I liked this movie"
. Otherwise return "This movie was not very good"
.
#Write your lambda function here
rate_movie = lambda rating: "I liked this movie" if rating > 8.5 else "This movie was not very good"
print(rate_movie(9.2))
print(rate_movie(7.2))
Ones' Place
You vsn use the modulo operator (%) with 10 to find the ones' place of an integer.
Here are some examples:
>>> 41%10
1
>>> 2%10
2
>>> 39%10
9
>>> 103%10
3
>>> 20%10
0
1.Create a lambda function named ones_place
that returns the ones’ place of the input num
.
#Write your lambda function here
ones_place = lambda num : num%10
print(ones_place(123))
print(ones_place(4))
Double Square
You can find the square of a number by multiplying it by itself:
eight_squared = 8*8
#The value of eight_squared is now 64
or by using the exponential operator **
:
seven_squared = 7**2
#The value of seven_squared is now 49
1.Create a lambda function named double_square
that takes an input named num
. The function should return twice the square of num
.
#Write your lambda function here
double_square = lambda num: 2 * num**2
print(double_square(5))
print(double_square(3))
Add Random
random.randint(a,b)
will return an integer between a
and b
(inclusive).
So, random.randint(5, 8)
could return any integer between 5
and 8
including both 5
and 8
.
random.randint(0, 100)
could return any integer between 0
and 100
including both 0
and 100
1.Create a lambda function named add_random
that takes an input named num
. The function should return num
plus a random integer number between 1 and 10 (inclusive).
import random
#Write your lambda function here
add_random = lambda num: num + random.randint(1,10)
print(add_random(5))
print(add_random(100))
Conclusion
You practiced basic lambda function syntax like:
add_two = lambda my_input: my_input+2
as well as more complicated lambda functions with if
/else
statements like:
check_if_A_grade = lambda grade: 'Got an A!' if grade >= 90 else 'Did not get an A...'
Also, along the way you practiced these core Python concepts:
- Strings and substrings
- Getting characters from strings
- Mathematical operations
- Comparators
- Random integers