-2

How to use the if elif else condition to insert the data into the existing column?

Example, if the columnA = 'abc' then columnB return '123', if the columnA = 'efg' then columnB return '345', else return '0'.

so based on the data it should be:

if the Sale id = 'sale001' then Sale no return 'clo sale', elif the Sale id = 'sale002' then Sale no return 'blo sale', else return ''.

enter image description here

beginofwork
  • 129
  • 8

1 Answers1

0

You don't need an "existing column" for computed/derived values.

Write a function with that logic.

def sale_no(sale_id):
  if sale_id == 'sale001':
    return 'clo sale'
  elif sale_id == 'sale002':
    return 'blo sale'
  return ''

Iterate over your data to call it to add a new column... pandas create new column based on values from other columns / apply a function of multiple columns, row-wise

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • getting error message `ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().` – beginofwork Nov 15 '22 at 03:31
  • That's not related to the code I've provided here, so I suggest opening a new post with the code you have. If you literally copied my answer, and don't understand the accepted answer in the link, then you must change to check `row['Sale id'] ==` – OneCricketeer Nov 15 '22 at 13:37
  • 1
    Got the correct result already. Facing another issue when put back to the data table. Will open a new post for the new issue. Thanks for your assistance @OneCricketeer – beginofwork Nov 16 '22 at 01:42