-1

I am trying to define a function then apply the function to the dataframe called reviews.

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

star_ratings = reviews.apply(lambda x: stars(x['points'], x['country']), axis=1)

I get the error:

<ipython-input-46-42befd5aa1a8> in stars(country, points)
      2     if country == 'Canada':
      3         return 3
----> 4     elif points >= 95:
      5         return 3
      6     elif points >= 85:

TypeError: '>=' not supported between instances of 'str' and 'int'

How should I rectify the code to make it work? Also, is the use of lambda here correct/common practice? Thanks!

odebear
  • 69
  • 1
  • 8
  • what is unclear about the error? Your data obviously contains `str`. Just convert them to numbers first. – Julien Aug 09 '21 at 04:09
  • Generally remapping based on conditions is done with `np.select` over the lambda. Like in [Pandas conditional creation of a series/dataframe column](https://stackoverflow.com/q/19913659/15497888) – Henry Ecker Aug 09 '21 at 04:18
  • 2
    You are sending the variables in reversed order than in the function definition. – Aryerez Aug 09 '21 at 04:19
  • 1
    What is the data type of the points column? Is it a string? It really helps to include a small sample dataframe that exhibits the question so we don't have to guess. – tdelaney Aug 09 '21 at 04:46

1 Answers1

0

Your point is in string so, you're getting this error. But you can solve this by changing that string into integer. And in function you are sending wrongly, it should be points and country rather country and points.

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

star_ratings = reviews.apply(lambda x: stars(x['points'], x['country']), axis=1)
imxitiz
  • 3,920
  • 3
  • 9
  • 33
  • How do you know that `points` is a string? OP doesn't give us enough information to know. – tdelaney Aug 09 '21 at 04:27
  • 1
    Given that the inputs were reversed it's likely that the `int` conversion is unnecessary and that this is just a typo by incorrectly mapping function arguments. – Henry Ecker Aug 09 '21 at 04:29
  • @HenryEcker How do you know that is unnecessary? Is there enough information to know that? So, I had gave answer which will work in any condition. And Yeah! I had explained OP is wrong in function argument and changed in code too. – imxitiz Aug 09 '21 at 04:34
  • @tdelaney Yeah! OP hadn't provided enough information so, I had gave information for either condition. – imxitiz Aug 09 '21 at 04:35
  • 1
    If the points column is a string, it would likely be better to fix the dataframe rather than trying to make up for it in support functions. – tdelaney Aug 09 '21 at 04:43
  • @tdelaney I agree with you. But it isn't clear.... And I believe OP's problem is already solved. – imxitiz Aug 09 '21 at 04:44