0
0
Pandasdata~5 mins

apply() with lambda functions in Pandas - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AAdds 1 to the first value only
BAdds 1 to the entire column
CRemoves 1 from each value in 'col'
DAdds 1 to each value in 'col'
Which apply() argument applies a function across rows in a DataFrame?
Aaxis=0
Baxis=1
Crow=True
Dcolumns=True
What type of function is commonly used inside apply() for quick operations?
ALambda function
BRecursive function
CGenerator function
DDecorator function
If you want to apply a function to each column in a DataFrame, which axis do you use?
Aaxis=0
Baxis=1
Caxis=2
Daxis=-1
Can apply() change the data type of a column?
AOnly for numeric columns
BNo, data types are fixed
CYes, if the function returns a different type
DOnly for string columns
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.