如何基于多个条件在df中创建新列?使用熊猫

在这里我需要在其他列中创建新列

样本数据:

colum1       column2

M           online

L           offline

C           online

L           online

H           online

M           online

L           offline

C           online

L           offline

在这里我需要创建新列

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

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

这样,我需要创建一个新列

尝试下面的代码,但我错过了逻辑

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

预期产量

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    
评论
  • iid
    iid 回复

    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)