1

I have a table like this enter image description here

I would like to replicate each row by the number of items in the 'Brand' column, and in the result table brand column would only have a single item, like this enter image description here

How to achieve this in Python? Thanks.

1 Answer 1

1

Try .explode:

df = df.assign(Brand=df.Brand.str.split(",")).explode("Brand")
print(df)

Prints:

        Date Brand    Cost
0  6/23/2021     A  $100.0
0  6/23/2021     B  $100.0
0  6/23/2021     C  $100.0
1  6/23/2021     A  $200.0
1  6/23/2021     B  $200.0
2  6/23/2021     A  $300.0
2  6/23/2021     B  $300.0
2  6/23/2021     C  $300.0
2  6/23/2021     D  $300.0
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for this clear simple solve!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.