how to replace column value with range in pandas dataframe

Viewed 6704

I have data frame called 'df' and I want to replace values within a range of columns in a data frame with a corresponding value in another column.

  1. 6 <= age < 11 then 1

    11 <= age < 16 then 2

    16 <= age < 21 then 3

    21 <= age then 4

            age
    86508   12.0
    86509   6.0
    86510   7.0
    86511   8.0
    86512   10.0
    86513   15.0
    86514   15.0
    86515   16.0
    86516   20.0
    86517   23.0
    86518   23.0
    86519   7.0
    86520   18.0
    

Results are

            age    stage
    86508   12.0    2
    86509   6.0     1    
    86510   7.0     1
    86511   8.0     1
    86512   10.0    1
    86513   15.0    2
    86514   15.0    2
    86515   16.0    2
    86516   20.0    3
    86517   23.0    4
    86518   23.0    4
    86519   7.0     1
    86520   18.0    3

Thanks.

2 Answers
Related