vendredi 16 août 2019

based on condition=True in an column filling random values to a particular column pandas python

I need to work on a column, and based on a condition (if it is True ), need to fill some random numbers for the entry(not a constant string/number ). Tried with for loop and its working, but any other fastest way to proceed similar to np.select or np.where conditions ?

I have written for loop and its working: The 'NUMBER' column have here few entries with greater than 4, i need to replace them by any float in between (120,123). I have used np.random.uniform and its working too.

    for i in range(0,len(data['NUMBER'])):
        if data['NUMBER'][i] >=1000:
        data['NUMBER'][i]=np.random.uniform(120,123)\

    '''The o/p for this code fills each entries with different values 
     between (120,123) in random,after replacement the entries are'''
     0          7.139093
     1         12.592815
     2         12.712103
     3        **120.305773**
     4         11.941386
     5         **122.548703**
     6         6.357255.............etc

    ''' but while using codes using np.select and np.where as shown below(as 
     it will run faster) --> the result was replaced by same number alone 
     for all the entries satisfying the condition. for example instead of 
     having different values for the indexes 3 and 5 as shown above it 
     have same value of any b/w(120,123 ) for all the entries. please 
     guide here.'''

    data['NUMBER'] =np.where(data['NUMBER'] >= 1000,np.random.uniform(120,123), data['NUMBER'])

    data['NUMBER'] = np.select([data['NUMBER'] >=1000],[np.random.uniform(120,123)], [data['NUMBER']])




Aucun commentaire:

Enregistrer un commentaire