Question about apply() function here

Hi! Can anyone help me out here? Thank you!!
The below code is part of the code from the solution. I have the below questions:

1.When we used mode(), we looked for the mode from the horizontally(from several columns), why did we do axis='columns' and why we quoted columns here? For my understanding, if we clarify columns inside the bracket, it means that we look for the mode vertically.
2,We put the function inside apply() why there is axis='columns'also? and why we quoted it instead of axis=1?

affordable_apps["Result"] = affordable_apps[criteria].mode(axis='columns')
def new_price(row):
    if row["affordability"] == "cheap":
        return round(max(row["Price"], cheap_mean), 2)
        return round(max(row["Price"], reasonable_mean), 2)
affordable_apps["New Price"] = affordable_apps.apply(new_price, axis="columns")```
1 Like

@candiceliu93 please provide a link.

1 Like

Look at the axis parameter in documentation, you can use both numbers 0/1 or words index/columns to indicate which of the 2 dimensions you want. Numbers are for users who want fast coding, words are for users who want readability (seems this readability is confusing you though). Not only this parameter/method/library, but many other API from scipy, numpy, python built-in datetime etc are designed to accept multiple formats. These make coding pipelines easier since upstream may send data as a list of 3 times, or a 3-tuple, or a string.

0/index means aggregate down the rows, 1/columns means aggregate across the columns. This doesn’t only apply to mode type aggregation. Create sample dataframes and aggregate to see for yourself and helps memory.

You need to specify which direction to aggregate, or else apply won’t know how to aggregate in a dataframe which is 2D.


Thank you!!It helps!!