-1

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
ah bon
  • 9,293
  • 12
  • 65
  • 148

2 Answers2

4

Just sum strings

"(" + df.categories + ")" + df.products
rafaelc
  • 57,686
  • 15
  • 58
  • 82
  • do you know why `df['format'] = '({0}){1}'.format(df.categories, df.products)` doesn't work in that way? – aydow Jun 27 '18 at 02:08
  • 1
    @aydow because you will be getting the result of the conversion of the arguments (in this case, the `Series` objects `df.categories` and `df.products`) to `string` and replacing it in the` {0}` and `{1}`. The result will be a single string and not a series as you probably intended – rafaelc Jun 27 '18 at 02:23
0

try this

    df = df.set_index('id')
    df  =df.apply(lambda x: "(" + x['categories'] + ")" + x['products'],axis=1).to_frame('products')
    print df
Dickster
  • 2,969
  • 3
  • 23
  • 29