pandas-python dataframe update a column

后端 未结 3 1414
隐瞒了意图╮
隐瞒了意图╮ 2021-01-21 23:52

Say I have a list BRANDS that contains brand names:

BRANDS = [\'Samsung\', \'Apple\', \'Nike\', .....]

Dataframe A has following structure

3条回答
  •  逝去的感伤
    2021-01-21 23:58

    You can achieve the result you are after by writing a simple function. You can then use .apply() with a lambda function to generate your desired column.

    def contains_any(s, arr):
        for item in arr:
            if item in s: return item
        return np.nan
    df['brand_name'] = df['product'].apply(lambda x: match_substring(x, product_map))
    

提交回复
热议问题