[Python] How to understand pandas .apply(axis='columns')?

Discussão em 'Python' iniciado por Stack, Maio 14, 2025.

  1. Stack

    Stack Membro Participativo

    Below is an answer code I received from Kaggle Pandas course.

    def stars(row):
    if row.country == 'Canada':
    return 3
    elif row.points >= 95:
    return 3
    elif row.points >= 85:
    return 2
    else:
    return 1

    star_ratings_2 = reviews.apply(stars, axis='columns')


    The question goes like this:


    We'd like to host these wine reviews on our website, but a rating system ranging from 80 to 100 points is too hard to understand - we'd like to translate them into simple star ratings. A score of 95 or higher counts as 3 stars, a score of at least 85 but less than 95 is 2 stars. Any other score is 1 star.


    Also, the Canadian Vintners Association bought a lot of ads on the site, so any wines from Canada should automatically get 3 stars, regardless of points.


    Create a series star_ratings with the number of stars corresponding to each review in the dataset.
    ​

    The dataset looks like this: Table

    My question is: star_ratings_2 = reviews.apply(stars, axis='columns') Why axis='columns instead of axis='rows'? since the stars() functions has to process country and points columns of a row, shouldn't we pass a row to the stars() function?

    I just didn't expect the correct answer will be axis='columns', I ve asked around including ChatGPT, but there is no good answer for me. ChatGPT even think that I am right where the axis='rows' should be correct.

    Continue reading...

Compartilhe esta Página