Skip to content
Advertisement

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

import pd as pandas
import random
import numpy as np 

data = {'Item_No':['001', '002', '003','004', '005', '006','007','008','009'], 
        'Group_code':[331, 332, 333, 333, 333, 331, 331, nan, nan]} 
df = pd.DataFrame(data)

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.

df['Group_Code'] = df['Group_Code'].apply(lambda v: (random.random() * 1000) if pd.isnull(v['Group_Code'] else v['Group_Code'], axis = 1))

Advertisement

Answer

Step 0:-

Your Dataframe:-

data = {'Item_No':['001', '002', '003','004', '005', '006','007','008','009'], 
        'Group_code':[331, 332, 333, 333, 333, 331, 331, np.nan, np.nan]} 
df = pd.DataFrame(data)

Step 1:-

Firstly define a function:-

def func(val):
    if pd.isnull(val):
        return random.random() * 1000
    else:
        return val

Step 2:-

Then just use apply() method:-

df['Group_code']=df['Group_code'].apply(func).astype(int)

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