1

Want to create a new column based on other column values.

Is there any faster way to calculate a new column?

Data= {'Result':['0.23','0.5','0.55','0.87','0.71','0.12','0.64']}

def risk_addr(x):
    if 1 >= x['Result'] > 0.75:
        return '11'          
    elif 0.75 >= x['Result'] > 0.5:
        return '10'
    elif 0.50 >= x['Result'] > 0.25:
        return '01'
    elif 0.25 >= x['Result'] > 0:
        return '00'
    return ''

Data['result_addr'] = Data.apply(risk_addr, axis=1)

Nirav Prajapati
  • 265
  • 2
  • 15

1 Answers1

1

Have you tried to bin.

First convert Results from string to float

df['Result']=df['Result'].astype(float)

Then put in bins

df['result_addr'] = pd.cut(df.Result, 
                       [0,0.25,0.5,0.75,1.0], 
                       labels=['00','01','10','11'])



    Result result_addr
0    0.23          00
1    0.50          01
2    0.55          10
3    0.87          11
4    0.71          10
5    0.12          00
6    0.64          10

It is good Friday so lets be nice. You could also use np.select([conditions],[choices]). Code below

df['Result']=df['Result'].astype(float)
c1=df['Result']> 0.75
c2=df['Result'] > 0.5
c3=df['Result'] > 0.25
c4=df['Result'] > 0


cond=[c1,c2,c3,c4]
choice=['11','10','01','00']

df['result_addr']=np.select(cond, choice)
wwnde
  • 26,119
  • 6
  • 18
  • 32