Replace float values with strings based on two different conditions

71 views Asked by At

In my df I have a column of results:

Results 
755
1065
2733
40
116
241
345
176
516
5000

What I would like to do is replace all values <=55 with the string Low and all values >=3500 with the string High, retaining all other values. So the end result would be:

Results 
755
1065
2733
Low
116
241
345
176
516
High

The issue is if you do a simple RM.loc[RM["Result"]<=55,"Result"]="Low", then it sets the entire column as strings and won't allow you to filter based on the second condition result>=3500. So I accomplished what I wanted by doing

RM.loc[RM["Result"]<=55,"Result"]=-111
RM.loc[RM["Result"]>=3511,"Result"]=999

RM.loc[RM["Result"]==-111,"Result"]="Low"
RM.loc[RM["Result"]==999,"Result"]="High"

But there must be a concise, one-line way of doing this I just can't think of it?

3

There are 3 answers

1
user19077881 On BEST ANSWER

Alternatively, in one line:

RM['results'] = RM['Result'].map(lambda x: 'Low' if x <= 55 else 'High' if x >= 3500 else x)
0
mozway On

Uses pre-computed masks, thus when you perform boolean indexing the dtype doesn't matter anymore:

m1 = RM["Results"]<=55
m2 = RM["Results"]>=3500

RM.loc[m1, "Results"]="Low"
RM.loc[m2, "Results"]="High"

Or, if you really want a one-liner, use numpy.select:

RM['Results'] = np.select([RM["Results"]<=55, RM["Results"]>=3500],
                          ['Low', 'High'], RM['Results'])

Output:

  Results
0     755
1    1065
2    2733
3     Low
4     116
5     241
6     345
7     176
8     516
9    High
0
rhug123 On

Here is a way using pd.cut()

pd.cut(df['Results'],bins = [0,55,3500,np.inf],labels = ['low','','high']).astype('str').where(lambda x: x.ne(''),df['Results'])

Output:

0     755
1    1065
2    2733
3     low
4     116
5     241
6     345
7     176
8     516
9    high