0

I have two data frames like the following:

Id    Name    Price   
1    [ABC]        33900
1    [XYZ]        33900

When I groupby using tolist by id, I got a list as:

Id   Name         Price
1    [[ABC],[XYZ]]   [33900,33900]

I want to convert this to single value as:

Id    Name     Price
1   [ABC,XYZ]   33900

I am not able to apply np.mean or any other operation as it is showing as list. Dataframe also included string which successfully grouped using tolist but integers also got added as list.

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252

1 Answers1

1

I think need GroupBy.apply with flattening lists:

df = (df.groupby(['Id','Price'])['Name']
        .apply(lambda x: [i for li in x for i in li])
        .reset_index())
print (df)
   Id  Price        Name
0   1  33900  [ABC, XYZ]
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252