0

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.

Tamil Selvan
  • 1,600
  • 1
  • 9
  • 25
Hans
  • 2,800
  • 3
  • 28
  • 40

1 Answers1

0
import numpy as np
a = np.where(df['parent_id'].isnull(), uuid.uuid4(), float("nan"))
df["parent_id"] = df['parent_id'].where(~df['parent_id'].isnull(), a)

Try something like this. I ussually float("nan") instead of None, even though this works with None as well.

Borut Flis
  • 15,715
  • 30
  • 92
  • 119