0

I am looking to create column which is created using multiple columns and multiple criteria. This column is the discount column which is created by some calculation on the price column TO explain the table below,

  1. if its brand A & B then discount column is 20% of price
  2. if its Unique id is 6352,7869 then if price between 60 to 70 then discount 10, if 70 to 80 discount 20
  3. if unique id 4094 and brand E and years_active blank then discount = price.

What is the best way to create this discount column.

This the data from the column that needs to be created is the discount column

Rebel_09
  • 13
  • 5
  • Post the image as text – Ynjxsjmh Mar 06 '23 at 05:17
  • 1
    Is the discount column percentage or absolute value? – Ashyam Mar 06 '23 at 05:18
  • You can do it with `np.select` – Ynjxsjmh Mar 06 '23 at 05:19
  • I think this should answer your question: https://stackoverflow.com/questions/30631841/how-do-i-assign-values-based-on-multiple-conditions-for-existing-columns/60244752 – Ashyam Mar 06 '23 at 05:21
  • Absolute value. – Rebel_09 Mar 06 '23 at 05:22
  • df.loc[(df['Brand'] == 'A') | (df['Brand'] == 'B'), 'discount'] = df.Price*0.2 def apply_discount(row): if row['Unique_id'] in [6352, 7869]: if row['Price'] >= 60 and row['Price'] < 70: return row['Price']*0.1 elif row['Price'] >= 70 and row['Price'] < 80: return row['Price']*0.2 return row['discount'] df['discount'] = df.apply(apply_discount, axis=1) df.loc[(df['Unique_id'] == 4094) | (df['years_active'] == None), 'discount'] = df.Price – Elkhan Mar 06 '23 at 05:42
  • there are multiple ways to create the discount column using: ```np.where``` or ```np.select``` or even write a custom ```def``` func using if statements – dramarama Mar 06 '23 at 06:48

0 Answers0