0

在这里我需要创建基于其他列的新列

样本数据:

colum1       column2

M           online

L           offline

C           online

L           online

H           online

M           online

L           offline

C           online

L           offline

在这里我需要创建新列

column1 = 'M' & colum2 = '在线' --> 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    
4

1 回答 1

1

您可以使用apply方法 -

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)
于 2020-05-18T05:22:01.957 回答