Hi,

I am stuck on my code for course 467-6. Apologies I could not tag the course for some reason.

My Code:

``````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.

Can you include your definition for `cheap_mean` and `reasonable_mean`

Here you are replacing `price_criterion` it should be assigning only `cheap` first and second time `reasonable` for that you can use `loc`

``````affordable_apps.loc[cheap, 'price_criterion'] = affordable_apps[cheap]['Price'].apply(
lambda x: 1 if x < cheap_mean else 0)

affordable_apps.loc[reasonable, 'price_criterion'] = affordable_apps[reasonable]['Price'].apply(
lambda x: 1 if x < reasonable_mean else 0)
``````

For later part instead of apply you can do

``````affordable_apps.loc[cheap, 'price_criterion'] = (affordable_apps[cheap]['Price'] < cheap_mean).astype(int)

affordable_apps.loc[reasonable, 'price_criterion'] = (affordable_apps[reasonable]['Price'] < reasonable_mean).astype(int)
``````
2 Likes

@YoussefAlaaEtman I just included it!

@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.

1 Like

Okay I see, thank you so much!

1 Like