I have a column in a dataframe
that has a value of None for a small number of rows. It's something like this:
id parent_id
1 1001
2 1001
3 1002
4 1002
5 None
Now I want to assign a uuid4()
to every record where the value is None
.
I'm currently using this method based on Using isnull() and groupby() on a pandas dataframe
df['uuid'] = df.apply(lambda _: uuid.uuid4(), axis = 1)
df['parent_id_na'] = df['parent_id'].fillna(df_asins['uuid'])
The downside is that generating a uuid
for every single value is very time consuming and most of them are thrown away later. Is there a better way to trigger an apply function on only those values where parent_id == None
I have tried below:
df = df.assign(parent_id = lambda x: x['parent_id'] if not (x['parent_id'].isnull) else uuid.uuid4())
But this returns a single uuid
for all values, even if there is a non-None value in parent_id
.