How to generate unique uuid in pandas without loop

Viewed 458

Currently I am generating unique uuid in each row using loop like this -

df['uuid'] = df.apply(lambda x: uuid.uuid4(), axis=1)

Is there way to do this without loop?

1 Answers

Remains a loop, but is a little faster than the current way

df['uuid'] = [uuid.uuid4() for x in range(df.shape[0])]

24.4 µs ± 2.31 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

Using apply

df['uuid'] = df.apply(lambda x: uuid.uuid4(), axis=1)

1.25 ms ± 39.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

Related