Uploaded July 2020 | Updated November 2026, 3 hours ago
This video covers the basics of creating user-defined functions in Python including lambda functions.
Subscribe:
► youtube.com/c/DataDaft?sub_confirmation=1
This is lesson 12 of a 30-part introduction to the Python programming language for data analysis and predictive modeling. Link to the code notebook below:
Python for Data Analysis: Functions
kaggle.com/hamelg/python-for-data-12-functions
This guide does not assume any prior exposure to Python, programming or data science. It is intended for beginners with an interest in data science and those who might know other programming languages and would like to learn Python.
I will create the videos for this guide such that you should be able to learn a lot just watching on YouTube, but to get the most out of the guide, it is recommended that you create a Kaggle account so that you can copy and edit each lesson so that you can follow along and run code yourself.
Introduction to Python Playlist:
youtube.com/playlist?list=PLiC1doDIe9rCYWmH9wIEYEXXaJ4KAi3jc
Link to the Python for Data Analysis written guide index page:
kaggle.com/hamelg/python-for-data-analysis-index .
⭐ Kite is a free AI-powered coding assistant that integrates with popular editors and IDEs to give you smart code completions and docs while you’re typing. It is a cool application of machine learning that can also help you code faster! Check it out here: kite.com/get-kite/?utm_medium=referral&utm_source=youtube&utm_campaign=datadaft&utm_content=description-only
This video covers the basics of creating user-defined functions in Python including lambda functions.
Subscribe:
► youtube.com/c/DataDaft?sub_confirmation=1
This is lesson 12 of a 30-part introduction to the Python programming language for data analysis and predictive modeling. Link to the code notebook below:
Python for Data Analysis: Functions
kaggle.com/hamelg/python-for-data-12-functions
This guide does not assume any prior exposure to Python, programming or data science. It is intended for beginners with an interest in data science and those who might know other programming languages and would like to learn Python.
I will create the videos for this guide such that you should be able to learn a lot just watching on YouTube, but to get the most out of the guide, it is recommended that you create a Kaggle account so that you can copy and edit each lesson so that you can follow along and run code yourself.
Introduction to Python Playlist:
youtube.com/playlist?list=PLiC1doDIe9rCYWmH9wIEYEXXaJ4KAi3jc
Link to the Python for Data Analysis written guide index page:
kaggle.com/hamelg/python-for-data-analysis-index .
⭐ Kite is a free AI-powered coding assistant that integrates with popular editors and IDEs to give you smart code completions and docs while you’re typing. It is a cool application of machine learning that can also help you code faster! Check it out here: kite.com/get-kite/?utm_medium=referral&utm_source=youtube&utm_campaign=datadaft&utm_content=description-only

![How to Use lapply, sapply and mapply in R
This video shows how to use the lapply, sapply and mapply functions to execute a function on each element of a list or vector in R. The apply family of functions provide a convenient way to quickly invoke a function on each element of vectors without having to write custom for loops.
Note that since data frames are lists where each column are separate elements of the list, the lapply and sapply will apply a function to each column when run on a data frame. This provides a quick way to create column-based summary statistics.
Code used in this clip:
# Example of lapply
data <- mtcars
# Function to apply
mpg_category <- function(mpg){
if(mpg > 30){
return(High)
} else if (mpg > 20){
return(Medium)
}
return(Low)
}
# Apply to each element
lapply(X = data$mpg, FUN = mpg_category)
# Use sapply to simplify the result to a vector or matrix instead of a list
sapply(X = data$mpg, FUN = mpg_category)
# You can pass additional arguments after FUN
within_range <- function(mpg, low, high){
if (mpg >= low & mpg <= high){
return(TRUE)
}
return(FALSE)
}
index <- sapply(X = data$mpg, FUN = within_range, low = 15, high = 20)
index
data[index,]
# Use mapply to apply a function along multiple vectors at the same time
# Function to apply
mpg_within_standard_range <- function(mpg, cyl){
if (cyl 4){
return(within_range(mpg, low = 23, high = 31))
} else if (cyl 6) {
return(within_range(mpg, low = 18, high = 23))
}
return(within_range(mpg, low = 13, high = 18))
}
index <- mapply(FUN = mpg_within_standard_range, mpg = data$mpg, cyl = data$cyl)
index
data[!index,]
# When used on a dataframe, lapply and sapply apply a function to each column
sapply(data, FUN = median)
* Note: YouTube does not allow greater than or less than symbols in the text description, so the code above will not be exactly the same as the code shown in the video! For R that means I may use = for assignment and the special Unicode large < and > symbols in place of the standard sized ones for dplyr pipes and comparisons. These special symbols should work as expected for R code on Windows, but may need to be replaced with standard greater than and less than symbols for other operating systems. How to Use lapply, sapply and mapply in R](https://i.ytimg.com/vi/34sbvhr_pm8/mqdefault.jpg)
![How to Select Columns Based on a Logical Condition in Pandas (Python)
↓ Code Available Below! ↓
This video shows how to select columns of a data frame based on a logical condition. Filtering or subsetting the columns of a data frame based on a logical check is not quite as common as filtering rows, but it can still be useful operation if you are only interested in variables that conform to certain conditions.
If you find this video useful, like, share and subscribe to support the channel!
► Subscribe: https://www.youtube.com/c/DataDaft?sub_confirmation=1
Code used in this Python Code Clip:
import pandas as pd
import statsmodels.api as sm #(To access mtcars dataset)
mtcars = sm.datasets.get_rdataset(mtcars, datasets, cache=True).data
mtcars.head()
# Create a logical index with one value for each column
logical_index = mtcars.mean() > 10
logical_index
# Get the corresponding columns
cols = mtcars.columns[logical_index]
# Use the column list to index the data frame
mtcars_sub = mtcars[cols]
mtcars_sub.head()
# Do logical indexing on columns in one line:
mtcars[mtcars.columns[mtcars.mean() > 10]].head()
* Note: YouTube does not allow greater than or less than symbols in the text description, so the code above will not be exactly the same as the code shown in the video! I will use Unicode large < and > symbols in place of the standard sized ones. .
⭐ Kite is a free AI-powered coding assistant that integrates with popular editors and IDEs to give you smart code completions and docs while you’re typing. It is a cool application of machine learning that can also help you code faster! Check it out here: https://www.kite.com/get-kite/?utm_medium=referral&utm_source=youtube&utm_campaign=datadaft&utm_content=description-only How to Select Columns Based on a Logical Condition in Pandas (Python)](https://i.ytimg.com/vi/3WtsZ1OxtfU/mqdefault.jpg)







