pandas-python dataframe update a column

后端 未结 3 1413
隐瞒了意图╮
隐瞒了意图╮ 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-22 00:14

    Vectorized solution:

    In [168]: x = df.item_title.str.split(expand=True)
    
    In [169]: df['brand_name'] = \
                  df['brand_name'].fillna(x[x.isin(BRANDS)]
                                             .ffill(axis=1)
                                             .bfill(axis=1)
                                             .iloc[:, 0])
    
    In [170]: df
    Out[170]:
       row     item_title brand_name
    0    1       Apple 6S      Apple
    1    2  Nike BB Shoes       Nike
    2    3     Samsung TV    Samsung
    3    4      Used bike        NaN
    

提交回复
热议问题