I have a df:
customer_id order_id product_id refund price
2QUKPQ3GB1 OE5Q7M1 WFL2ILKU3Z False 692
ISGF2N3OQL I8E6PGG WFL2ILKU3Z False 668
ISGF2N3OQL I8E6PGG WFL2ILKU3Z True 541
I have 2 functions that return a random positive or negative comment with the following structure:
def random_negative_sentence():
# ommiting the list of random words
product = random.choice(products)
adj = random.choice(negative)
color = random.choice(colors)
who = random.choice(person)
verb = random.choice(negative_verbs)
end = random.choice(l_neg)
sentence1 = f'{who} {verb} the {color} {adj} {product}. {end}.'
sentence2 = f'The {color} {product} is so {adj} that even {who} {verb} it! ' + end
sentence3 = f'{who} {verb} the {color} {product} because it is so {adj}! ' + end
sentence4 = f'{who} {verb} the {color} {product} because it is {adj} and { random.choice(negative)}! ' + end
return random.choice([sentence1, sentence2, sentence3, sentence4])
random_positive_sentence()
# my kids hate the blue rocking char because it is hard to assemble and clumsy! That was the last time I used this service
I am now trying to assign a random review to my df depending if it was refunded or not. I am able to do this using df['review'] = df['refund'].apply(lambda x: random_positive_sentence() if x == False else random_negative_sentence())
But then I realised that for the same product_id the review is using a different product, so I still want to assign everything randomly depending on the refund but the product should be the same per product_id column. So that all of the randomly generated reviews would be about the same product, but reviews would still be different.
How can I assign reviews depending on the refund column per groups of product_id?
So that the end result would look like this:
customer_id order_id product_id refund price review
2QUKPQ3GB1 OE5Q7M1 WFL2ILKU3Z False 692 'I love the zaffre sturdy rocking char. Will recommend to my coworkers'
ISGF2N3OQL I8E6PGG WFL2ILKU3Z False 668 'my grandmom appreciate the amber rocking char because it is reliable and easy to move! Will recommend to my family.'
ISGF2N3OQL I8E6PGG WFL2ILKU3Z True 541 'my grandpa abhor the yellow hard to assemble rocking char. I will never use this service again.'