cheap = affordable_apps["Price"] < 5
reasonable = affordable_apps["Price"] >= 5
cheap_mean = affordable_apps[cheap]['Price'].mean()
reasonable_mean = affordable_apps[reasonable]['Price'].mean()
affordable_apps['price_criterion'] = affordable_apps[cheap]['Price'].apply(
lambda x: 1 if x < cheap_mean else 0)
affordable_apps['price_criterion'] = affordable_apps[reasonable]['Price'].apply(
lambda x: 1 if x < reasonable_mean else 0)
I was expecting to fill the affordable_apps[âprice_criterionâ] table with the correct value of 1 or 0. But for some reason for it does not fill for the âcheapâ boolean mask.
@DishinGoyani thank you for the answer, I understand that is the same answer provided in the exercise.
However, may you explain why my code doesnât work? I thought writing affordable_apps[cheap][âPriceâ].apply() would help filter the columns within price_criterion. Is there a way to write my code to make it work?
Expression on right hand side is correct but issue is in left hand side (While assigning result to column)
Both time you are using affordable_apps['price_criterion'] = ... that will replace all column instead of altering only cheap rows. To made it only change cheap rows we will need to pass boolean index on left hand side. like affordable_apps.loc[cheap, 'price_criterion'] = ... so only cheap rows get alter. and the same for reasonable rows.