Assigning different randomly generated values to each rows (python)

129 Views Asked by At

I am trying to make a dummy data where I would like to assign random sentences under the question column.

I am using from essential_generators import DocumentGenerator to generate the random sentences.

for i, row in dummy.iterrows():
    dummy['Question'] = gen.sentence()

I thought if I iterate each row and apply gen.sentence(), which randomly generates a sentence each time, i would get different sentences for my data of 1000 rows. However, it is result in giving me the same sentence for all 1000 rows.

What can I do to yield my desired result?

3

There are 3 best solutions below

0
On
dummy['Question'] = dummy.apply(lambda row: gen.sentence(), axis=1)
0
On
for i, row in dummy.iterrows():
    row['Question'] = gen.sentence()

This is probably what you're looking for. I can't advise more as I'm not getting enough information from your question.

5
On

dummy['Question'] = [gen.sentence() for _ in dummy.index]

EDIT: on the right-hand side we use list comprehension to create a list with len(dummy.index) number of elements. We iterate over dummy.index. _ by convention is just throw-away variable name - i.e. we don't care about it. we just need to call gen.sentence certain number of times.