Price vs category and Genres

Screen Link: Learn data science with Python and R projects

My Code:

genres_mean = affordable_apps.groupby(
    ["affordability", "genre_count"]

def label_genres(row):
    """For each segment in `genres_mean`,
    labels the apps that cost less than its segment's mean with `1`
    and the others with `0`."""

    aff = row["affordability"]
    gc = row["genre_count"]
    price = row["Price"]

    if price < genres_mean.loc[(aff, gc)][0]:
        return 1
        return 0

When I apply the function into affordable_apps, will it compare the relevant affordability and category in categories_mean? for instance, if the price belongs to ‘cheap’, ‘genre_count’ is 2, it will compare the price in ‘cheap’, ‘genre_count’ is 2 in genres_mean.?

Also, i don’t understand why it added [0] in the if sentences (if price < genres_mean.loc[(aff, gc)][0])?


I don’t quite understand what you’re asking here… but perhaps understanding what this bit of code does might clarify things for you:

genres_mean = affordable_apps.groupby(
    ["affordability", "genre_count"]

This will create a dataframe with a single column and a multi index that will show the mean price for each group that can be made from affordability and genre_count. This also explains your second question because using genres_mean.loc[(aff, gc)] will return a pandas series and by adding [0] to the end allows us to get the values for the corresponding (aff, gc) group. We need the raw value in order to compare it to price in our if statement.

Try using the variable inspector in the code window to look at what is being stored in genres_mean to get a better understanding of this object.

Thank you Mike, I think I understand now after reading your explanation several times.