Recall & Review
beginner
What does the
apply() function do when used on DataFrame columns in pandas?It applies a given function to each column (Series) of the DataFrame, allowing you to transform or analyze each column separately.
Click to reveal answer
beginner
How do you apply a function to each column of a DataFrame using
apply()?Use
df.apply(your_function). By default, apply() works column-wise, passing each column as a Series to the function.Click to reveal answer
intermediate
What is the difference between
apply() and applymap() in pandas?apply() works on rows or columns (Series), while applymap() works element-wise on every single value in the DataFrame.Click to reveal answer
beginner
Example: How to calculate the range (max - min) of each column using
apply()?Use
df.apply(lambda col: col.max() - col.min()). This applies the lambda function to each column and returns a Series of ranges.Click to reveal answer
intermediate
Can
apply() on columns handle functions that return multiple values?Yes, if the function returns a Series or list,
apply() will combine these into a DataFrame with new columns.Click to reveal answer
What is the default axis when using
apply() on a pandas DataFrame?✗ Incorrect
By default, apply() works on columns, which is axis=0 in pandas.
Which pandas function applies a function element-wise to every cell in a DataFrame?
✗ Incorrect
applymap() applies a function to each element in the DataFrame.
What will
df.apply(lambda col: col.mean()) return?✗ Incorrect
The lambda function calculates the mean of each column because apply() works column-wise by default.
If a function passed to
apply() returns a Series, what is the output?✗ Incorrect
When the function returns a Series for each column, apply() combines these into a DataFrame.
How can you apply a function to each row instead of each column using
apply()?✗ Incorrect
Setting axis=1 makes apply() work row-wise.
Explain how the
apply() function works on DataFrame columns in pandas and give a simple example.Think about how you can transform or summarize each column separately.
You got /3 concepts.
Describe the difference between
apply() and applymap() in pandas.Consider the level at which the function is applied: whole columns vs individual cells.
You got /3 concepts.