Understanding part of the code logic

I mostly understanding how the axes work when indexing (i.e., rows = 0, columns = 1). I generally try to say the logic in my head (unsure if this is a good way to learning programming). But from a previous lesson:

def percentages(col):
    div = col/happiness2015["Happiness Score"]
    return div*100

factor_percentages = happiness2015.loc[:,factors].apply(percentages)

I understand this as “down these rows, for the columns in factors, apply percentages function at one column at a time”.

However for the lesson in this tag:

def cheap_reas(row):
    if row["Price"] < 5:
        return "cheap"
    else:
        return "reasonable"

affordable_apps["affordability"] = affordable_apps.apply(cheap_reas, axis = 1)

I don’t fully understand the logic of using axis 1/ can’t explain it well.

Is it because of the nature of df.apply is to apply a function on a column axis (axis =1) one at a time and the function is referencing a specific column, “Price” makes it so we have to pick axis = 1?

hey @AWM007

When it comes to dataframe.apply() method the default value for axis is 0 that is Column.

Since the function has been written to be applied to each row for the column [Price], axis has been changed to 1. It’s like customizing apply method based on what is required.

you may refer to the official doc for a detailed explanation.

Thanks. Just so I understand, and reiterate to make sure I get it:

In example 1) The function is being applied along axis = 0 (column by column). Therefore, the apply method is also applied along axis = 0 (column by column).

In example 2) The function is being applied along axis 1 (row by row). Therefore, the apply method is also applied along axis = 1 (row by row), and SAVED as a new column. (I think it was this part that was throwing me off)

hey @AWM007

yup.

  1. doesn’t have axis = 0 option explicitly added to the apply method since it is the default.
  2. axis = 1 is supplied to apply method otherwise it will try to transform the values by column.