0

I have used the following code to display top 3 products of each retailer

baseline_df = baseline_df.groupby(['RtrName'])['Product'].agg(lambda x:x.value_counts().index[0:3]).to_frame().reset_index()

The result of this is a dataframe with two columns - RtrName and Product but each row in the Product column has 3 values split by a comma (,)

Result Output:

RtrName  Product
RtrName_a Product1,Product2,Product3
RtrName_b Product1,Product2,Product3

I want to have my dataframe in a way like : Expected Output

RtrName_a  Product1
           Product2
           Product3 
RtrName_b  Product1
           Product2
           Product3

How do I achieve this?

2 Answers 2

2

Use DataFrame.explode of splitted values by Series.str.split before your solution:

baseline_df = (baseline_df.assign(Product = baseline_df.Product.str.split(','))
                          .explode('Product')
                          .groupby(['RtrName'])['Product']
                          .agg(lambda x:x.value_counts().index[:3])
                          .reset_index(name='new'))

Different output is if use explode after groupby:

baseline_df = (baseline_df.groupby(['RtrName'])['Product']
                          .agg(lambda x:x.value_counts().index[:3])
                          .str.split(',')
                          .explode()
                          .reset_index(name='Product')) 
3
  • Hi! ValueError: columns must be unique. When I tried the first code sample.
    – coder_bg
    Feb 15, 2021 at 10:29
  • No, not happening. It says : ValueError: columns must be unique
    – coder_bg
    Feb 15, 2021 at 16:02
  • I have edited my question. Showing the resultant output vs expected output.
    – coder_bg
    Feb 15, 2021 at 16:05
0

So I tried it this way and achieved what I wanted.

def func(x):
    x = x[:10]
    return x['Product']
    
temp = baseline_df.groupby('RtrName').apply(lambda x: func(x))
temp = temp.to_frame().reset_index().drop(columns=['level_1'])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.