Reputation: 347
This is a watered down version of my situation. I am working in a code environment I cannot modify. On the back end, there is a function that resembles add_sigma
. I am needing to assign the function to execute across rows where the "value" column holds a particular string. The function itself forces me into a boolean expression. Any way around this?
import pandas as pd
import numpy as np
def add_sigma(percent):
if percent >= 99.9997:
return 6
elif percent <= 99.9996 and percent >= 99.98:
return 5
elif percent <= 99.979 and percent >= 99.4:
return 4
elif percent <= 99.39 and percent >= 93.3:
return 3
elif percent <= 93.29 and percent >= 69.1:
return 2
elif percent <= 69.09 and percent >= 30.9:
return 1
elif percent <= 30.89 and percent >= 0:
return 0
df = pd.DataFrame({'value': ['0-3 bucket', '4-7 bucket', '8+ bucket', '0-3 bucket', '0-3 bucket', '8+ bucket'],
'percentage': [27.68, 82.94, 32.26, 91.97, 99.82, 67.44]})
df['sigma'] = ''
df['sigma'] = np.where(df['value'] == '0-3 bucket', add_sigma(df['percentage']), df['sigma'])
expected output would be
value percentage sigma
0 0-3 bucket 27.68 0
1 4-7 bucket 82.94
2 8+ bucket 32.26
3 0-3 bucket 91.97 2
4 0-3 bucket 99.82 4
5 8+ bucket 67.44
Upvotes: 1
Views: 44
Reputation: 8508
Like cs95 said, you can use conditions and call the add_sigma function. One way to do it is to include the condition inside the apply like this:
df['sigma'] = df.apply(lambda x: add_sigma(x.percentage) if x['value'] == '0-3 bucket' else '',axis=1)
The result will be:
value percentage sigma
0 0-3 bucket 27.68 0
1 4-7 bucket 82.94
2 8+ bucket 32.26
3 0-3 bucket 91.97 2
4 0-3 bucket 99.82 4
5 8+ bucket 67.44
Using .loc, you can do :
df.loc[df['value'] == '0-3 bucket' , 'sigma'] = df['percentage'].map(add_sigma)
df['sigma'] = df['sigma'].fillna('')
Upvotes: 1