Skip to content
Advertisement

Pandas apply unique random number to nan else go to next row

JavaScript

I would like to apply a unique random number to ‘nan’ and keep the group code where group code exists. I’ve tried the following, but i cant seem to get the syntax right, what am i doing wrong.

JavaScript

Advertisement

Answer

Step 0:-

Your Dataframe:-

JavaScript

Step 1:-

Firstly define a function:-

JavaScript

Step 2:-

Then just use apply() method:-

JavaScript

Now if you print df you will get your expected output

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement