Lecture 9
Functions
DATA 8
Spring 2024
Announcements
Histograms
Area and Height
Area of bar = % in bin = Height x width of bin
(Demo)
Charts Wrap Up
Summary
(Demo)
Discussion Question
You have data about daily temperatures as shown. Which type of chart would show the answer to each question?
Defining Functions
Def Statements
User-defined functions give names to blocks of code
def spread(values):
return max(values) - min(values)
(Demo)
Name
Argument names (parameters)
Body
Return expression
Discussion Question
What does this function do? What kind of input does it take? What output will it give? What's a reasonable name?
def f(s):
return np.round(s / sum(s) * 100, 2)
(Demo)
Apply
Apply
apply
table_name.apply(function_name, 'column_label(s)')
(Demo)