I need to perform conditional calculations on 2 columns. The rules are the same. I have been using two functions and applying them to each column, as shown below.
df = pd.DataFrame({'Min': [50, 50],
'Max' : [150, 150],
'Rule': ['A', 'B']})
def adjust_min(row):
if row['Rule'] == 'A':
return row['Min'] * 5
elif row['Rule'] == 'B':
return row['Min'] * 10
else:
return row['Min']
def adjust_max(row):
if row['Rule'] == 'A':
return row['Max'] * 5
elif row['Rule'] == 'B':
return row['Max'] * 10
else:
return row['Max']
df['Min'] = df.apply(adjust_min, axis=1)
Ideally, I would want a function that applies to both columns, perhaps:
if row['Rule'] == 'A':
return row * 5
Is there a more efficient way to do this? Thank you!