Home > other >  Pandas Dataframe: Replace values in a column based on certain Range in values of the same column
Pandas Dataframe: Replace values in a column based on certain Range in values of the same column

Time:07-29

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.

CodePudding user response:

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)
  • Related