0

I have a simple DataFrame like the following:

lead Rating
Clint 2
Saoro 1
Clint 3
Saoro 4
Clint 5
Clint 6
billy 9
Clint 10

I want to Replace rating column values as 1-3 - low, 4-6 - average, 7-8 - Good, 9-10 - Excellent

I tried with,

    df['Rating'].mask(df['Rating'] <=3 ,'low', inplace=True)
    df3['Rating'].mask((df3['Rating'] >=4) | (df3['Rating'] < 7) ,'average', inplace=True)

but this will give error " TypeError: '>=' not supported between instances of 'str' and 'int'" as it is taking into consideration the firstly replaced column value "low" also in the second line. Its the same when I tried using lamba function also.

1
  • Please check as Image is not available. Please reattach the image. Also, try to change data type of column Rating from String to Integer and It should work. use "astype" method of dataframe to do so. Commented Jul 28, 2022 at 8:22

1 Answer 1

4

In case the values in column Rating are strings: df["Rating"] = df["Rating"].astype("int")

First option - .replace() with a mapping dictionary:

mapping = {
    1: 'low', 2: 'low', 3: 'low',
    4: 'average', 5: 'average', 6: 'average',
    7: 'good', 8: 'good',
    9: 'excellent', 10: 'excellent'
}
df["Rating"] = df["Rating"].replace(mapping)

Second option - .map() with a mapping function:

def mapping(rating):
    if rating <= 3:
        return "low"
    if rating <= 6:
        return "average"
    if rating <= 8:
        return "good"
    return "excellent"

df["Rating"] = df["Rating"].map(mapping)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.