Recall & Review
beginner
What does the
apply() function do in pandas?It lets you run a function on each row or column of a DataFrame or on each element of a Series, making it easy to transform data.
Click to reveal answer
beginner
How do lambda functions help when using
apply()?Lambda functions let you write small, quick functions directly inside <code>apply()</code> without needing to define a separate function first.Click to reveal answer
beginner
Example: What does this code do?<br>
df['new'] = df['old'].apply(lambda x: x * 2)
It creates a new column called 'new' where each value is double the corresponding value in the 'old' column.
Click to reveal answer
intermediate
What argument do you use with
apply() to apply a function across rows instead of columns?Use
axis=1 to apply the function across rows. The default is axis=0 for columns.Click to reveal answer
beginner
Can
apply() with lambda functions be used on both Series and DataFrames?Yes,
apply() works on Series to apply a function to each element, and on DataFrames to apply a function to each column or row.Click to reveal answer
What does
df['col'].apply(lambda x: x + 1) do?✗ Incorrect
The lambda function adds 1 to each element in the 'col' column.
Which
apply() argument applies a function across rows in a DataFrame?✗ Incorrect
axis=1 applies the function across rows.What type of function is commonly used inside
apply() for quick operations?✗ Incorrect
Lambda functions are small, quick functions often used inside
apply().If you want to apply a function to each column in a DataFrame, which axis do you use?
✗ Incorrect
axis=0 applies the function to each column.
Can
apply() change the data type of a column?✗ Incorrect
If the function returns a different type, the column's data type can change.
Explain how
apply() works with lambda functions on a pandas DataFrame.Think about how you can quickly change data in a table using small functions.
You got /4 concepts.
Describe a real-life example where you might use
apply() with a lambda function in data analysis.Imagine you have a list of prices and want to add tax to each price.
You got /4 concepts.