When to use .apply() function to select rows based on a criteria


I was trying to solve the practice exercise here - Pandas - Inner Merge

The question is:
Cool T-Shirts Inc. wants to know the months when they crushed their targets.
Select the rows from sales_vs_targets where revenue is greater than target . Save these rows to the variable crushing_it .

I answered like this crushing_it = sales_vs_targets.apply(sales_vs_targets.revenue > sales_vs_targets.target) but it was wrong.

The correct solution is: crushing_it = sales_vs_targets[sales_vs_targets.revenue > sales_vs_targets.target]

From what I understand, .apply() can be used to perform operations on a dataframe, so I am not sure why my answer is wrong. May I ask… when should I use .apply() to perform operations then? Appreciate an explanation.

Example of .apply()

Thanks and cheers!

I think apply() is used to modify a specific data in your column. For example df[‘name’] = df.name.apply(get_last_name) will modify the column ‘name’ to only obtain the ‘last name’ of each ‘name’. While sales_vs_targers[sales_vs_targets.revenue > sales_vs_targets.target] is used to select rows given the particular condition, in this case (sales_vs_targets.revenue > sales_vs_targets.target)~. Hope this helps PS happy coding!