Search code examples
pythonpandasmappingbinning

Mapping the binned data in Pandas


df12.head()
            COMPONENT_ID   PSRO       binned   PSRO_SPEED
4080  11S02CY383YH1934794910  7.470  (7.4, 7.65]  (7.4, 7.65]
4722  11S02CY388YH1934786330  7.491  (7.4, 7.65]  (7.4, 7.65]
4708  11S02CY388YH1934782718  7.497  (7.4, 7.65]  (7.4, 7.65]
4726  11S02CY388YH1934786336  7.564  (7.4, 7.65]  (7.4, 7.65]
4707  11S02CY388YH1934782709  7.581  (7.4, 7.65]  (7.4, 7.65]

I want that binned data to map to different values. I tried

df12['PSRO_SPEED']=df12['PSRO_SPEED'].map({'(7.4,7.65]': 'high_speed'})

But this is not working. It is changing df12['PSRO_SPEED'] to NAN.


Solution

  • I think it is Interval, not string, so possible solution is:

    i = pd.Interval(7.4,7.65, closed='right')
    df12['PSRO_SPEED']=df12['PSRO_SPEED'].map({i: 'high_speed'})
    

    Or for your solution is necessary convert column to strings:

    df12['PSRO_SPEED']=df12['PSRO_SPEED'].astype(str).map({'(7.4,7.65]': 'high_speed'})
    

    But better should be add parameter label to cut:

    bins = [7.4,7.65,7.9,8.15,8.4,8.65] 
    labels = ['lowest','low','medium','great','greatest']
    df12['binned'] = pd.cut(df12['PSRO'], bins=bins, labels=labels)