combine one string column to another with Pandas

63 views Asked by At

If i have a following dataframe:

id     categories      products
01        fruit       Apple, Apricot
02        fruit       Apple, Banana, Clementine, Pear
03        fruit       Orange, Pineapple, Pear
04      vegetable     Carrot, Cabbage

and i want create a new df like this, what i should do? thanks.

id          products
01     (fruit)Apple, Apricot
02     (fruit)Apple, Banana, Clementine, Pear
03     (fruit)Orange, Pineapple, Pear
04     (vegetable)Carrot, Cabbage
2

There are 2 answers

3
Dickster On BEST ANSWER

try this

    df = df.set_index('id')
    df  =df.apply(lambda x: "(" + x['categories'] + ")" + x['products'],axis=1).to_frame('products')
    print df
2
rafaelc On

Just sum strings

"(" + df.categories + ")" + df.products