如何使用python DataFrame中的条件创建新列?

时间:2019-01-02 10:38:44

标签: python-3.x dataframe

只需将R代码转换为等效的Python代码。

Item_Type-旧列名 Item_Type_new-新列名

perishable = c("Breads", "Breakfast", "Dairy", "Fruits and Vegetables", "Meat", "Seafood")

non_perishable = c("Baking Goods", "Canned", "Frozen Foods", "Hard Drinks", "Health and Hygiene", "Household", "Soft Drinks")

# create a new feature 'Item_Type_new'
combi[,Item_Type_new := ifelse(Item_Type %in% perishable, "perishable", ifelse(Item_Type %in% non_perishable, "non_perishable", "not_sure"))]

2 个答案:

答案 0 :(得分:2)

通过简单的功能,您可以在熊猫数据框上apply

def func(x, l1, l2):
    """
    x = input value
    l1 = list of perishables
    l2 = list of non-perishables
    """    
    if x in l1:
        return 'perishable'
    elif x in l2:
        return 'non-perishable'
    else:
        return 'not_sure'


perishable = ["Breads", "Breakfast", "Dairy", "Fruits and Vegetables", "Meat", "Seafood"]
non_perishable = ["Baking Goods", "Canned", "Frozen Foods", "Hard Drinks", "Health and Hygiene", "Household", "Soft Drinks"]

combi['Item_Type_new'] = combi.apply(lambda x: func(x, perishable, non_perishable), axis=1)

答案 1 :(得分:0)

使用np.select()-

perishable = ["Breads", "Breakfast", "Dairy", "Fruits and Vegetables", "Meat", "Seafood"]

non_perishable = ["Baking Goods", "Canned", "Frozen Foods", "Hard Drinks", "Health and Hygiene", "Household", "Soft Drinks"]

conditions = [
    (combi['Item_Type'].isin(perishable)),
    (combi['Item_Type'].isin(non_perishable))]

choices = ['perishable', 'non_perishable']

combi['Item_Type_new'] = np.select(conditions, choices, default='non_perishable')