I'm looking to add a uuid for every row in a single new column in a pandas DataFrame. This obviously fills the column with the same uuid:
import uuid
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(4,3), columns=list('abc'),
index=['apple', 'banana', 'cherry', 'date'])
df['uuid'] = uuid.uuid4()
print(df)
a b c uuid
apple 0.687601 -1.332904 -0.166018 34115445-c4b8-4e64-bc96-e120abda1653
banana -2.252191 -0.844470 0.384140 34115445-c4b8-4e64-bc96-e120abda1653
cherry -0.470388 0.642342 0.692454 34115445-c4b8-4e64-bc96-e120abda1653
date -0.943255 1.450051 -0.296499 34115445-c4b8-4e64-bc96-e120abda1653
What I am looking for is a new uuid in each row of the 'uuid' column. I have also tried using .apply() and .map() without success.
This is one way:
df['uuid'] = [uuid.uuid4() for _ in range(len(df.index))]