Help - Python pandas dataframe with user define functions

Hi,

I am doing a question:

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.

Write a function that takes a score and a country as parameters and returns the respective star value. Display the country, variety, points, and star rating.

The dataset contains the following columns:

  1. country
  2. description
  3. designation
  4. points
  5. price
  6. province
  7. region_1
  8. region_2
  9. taster_name
  10. taster_twitter_handle
  11. title
  12. variety
  13. winery

Currently, i could only get the following:

def star_rating(score,country):
    if dfwine['country'] == 'Canada':
        rating = 3
    elif points>=95:    
        rating = 3
    elif points <95 and points >= 85:
        rating = 2
    else:
        rating = 1
    return rating

But I am stuck with what to do afterwards, could anyone please help?

Thank you very much in advance.

Do you have a link to the CC lesson?

Hi @lisalisaj,

Thank you very much for replying.

Sorry I do not have a link but its similar to the following questions in the website:

Its Exercise 7 but my work requires me to “Write a function that takes a score and a country as parameters and returns the respective star value. Display the country, variety, points, and star rating.

Thank you!

Is this for a school test?

What is the result of what you’ve written so far? What’s your error message?

Hi @lisalisaj,

It’s not for a school test, it is an assignment.

Sorry i could not get any answer when i run it as i am not sure how to continue and my codes are incomplete.

Nothing is printing b/c you haven’t passed any arguments or called the function.
I don’t have access to that csv file. But, I did write out a function with points & country as the parameters like so:

def star_rating(points, country):

So, let’s think about this a different way for starters.
Pretend there’s no pandas df. How would you write a function with those two parameters that would work when passing in any country or points arguments when the function is called?
Ex:

print(star_rating(96, 'France'))  
print(star_rating(88, 'Canada'))  

How would that logic look in the function (considering that if the country is Canada and no matter what their points value is they will always get 3 stars, and, 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)?
Try that, write it out if it helps. (I’m also assuming that this exercise builds on the previous ones, so the clues are there.)

1 Like

Hi @lisalisaj,

Thank you for your time & reply.

It’s alright, I have managed to solve it with some samples that I could find.

Thank you!

1 Like