One approach with np.where is
mask = df.Category.values=='small' df['Discount'] = np.where(mask,df.price*0.01, df.price*0.02)
Another way to put things differently is
df['Discount'] = df.price*0.01 df['Discount'][df.Category.values!='small'] *= 2
For performance, you may need to work with array data, so we could use df.price.values instead of using df.price tags.
Benchmarking
Approaches -
def app1(df): # Proposed app#1 here mask = df.Category.values=='small' df_price = df.price.values df['Discount'] = np.where(mask,df_price*0.01, df_price*0.02) return df def app2(df): # Proposed app#2 here df['Discount'] = df.price.values*0.01 df['Discount'][df.Category.values!='small'] *= 2 return df def app3(df): # @piRSquared soln df.assign( Discount=((1 - (df.Category.values == 'small')) + 1) / 100 * df.price.values) return df def app4(df): # @MaxU soln df.assign(Discount=df.price * df.Category.map({'small':0.01}).fillna(0.02)) return df
Dates -
1) Big data set:
In [122]: df Out[122]: Category ID price Discount 0 small 1 25 0.25 1 medium 2 30 0.60 2 medium 3 34 0.68 3 small 4 40 0.40 In [123]: df1 = pd.concat([df]*1000,axis=0) ...: df2 = pd.concat([df]*1000,axis=0) ...: df3 = pd.concat([df]*1000,axis=0) ...: df4 = pd.concat([df]*1000,axis=0) ...: In [124]: %timeit app1(df1) ...: %timeit app2(df2) ...: %timeit app3(df3) ...: %timeit app4(df4) ...: 1000 loops, best of 3: 209 µs per loop 10 loops, best of 3: 63.2 ms per loop 1000 loops, best of 3: 351 µs per loop 1000 loops, best of 3: 720 µs per loop
2) A very large data set:
In [125]: df1 = pd.concat([df]*10000,axis=0) ...: df2 = pd.concat([df]*10000,axis=0) ...: df3 = pd.concat([df]*10000,axis=0) ...: df4 = pd.concat([df]*10000,axis=0) ...: In [126]: %timeit app1(df1) ...: %timeit app2(df2) ...: %timeit app3(df3) ...: %timeit app4(df4) ...: 1000 loops, best of 3: 758 µs per loop 1 loops, best of 3: 2.78 s per loop 1000 loops, best of 3: 1.37 ms per loop 100 loops, best of 3: 2.57 ms per loop
Further enhancing data reuse -
def app1_modified(df): mask = df.Category.values=='small' df_price = df.price.values*0.01 df['Discount'] = np.where(mask,df_price, df_price*2) return df
Dates -
In [133]: df1 = pd.concat([df]*10000,axis=0) ...: df2 = pd.concat([df]*10000,axis=0) ...: df3 = pd.concat([df]*10000,axis=0) ...: df4 = pd.concat([df]*10000,axis=0) ...: In [134]: %timeit app1(df1) 1000 loops, best of 3: 699 µs per loop In [135]: %timeit app1_modified(df1) 1000 loops, best of 3: 655 µs per loop