0

Here I need to create new column based in other columns

sample Data:

colum1       column2

M           online

L           offline

C           online

L           online

H           online

M           online

L           offline

C           online

L           offline

Here I need to create new column

column1 = 'M' & colum2 = 'online' --> 3 days

Column1 = 'M' & colum2 = 'offline' --> 5days

Like this I need to create new column

tried below code but I missed logic

sales['Shipment Tat'] = np.where ((sales['Order Priority'] == 'M') & (sales['Sales Channel'] == 'Online') ,  'with in 9 days' )

expected output

column1    column2       column3

M           online     3 days

M           offline    5 days

L           offline    5 days

C           online     7 days

L           online     7 days

H           online     9 days

H           offline    11 days    
ggorlen
  • 44,755
  • 7
  • 76
  • 106

1 Answers1

1

You can use apply method -

def myFunc(record):
    if record['column1'] == 'M':
       if record'column2'] == 'online':
          return '3days'
       elif record['column2'] == 'offline':
          return '5days'
    return 'with in 9 days'

df['new_col'] = df.apply(myFunc, axis=1)
Tom Ron
  • 5,906
  • 3
  • 22
  • 38