0

My two columns having 13961 rows, based on the value in one column, i need to change the value in another column.

  1. if data[CO BORROWER NAME'] have marked as 'NOT_AVAILABLE' my other column data['CO BORROWER_STATUS'] should be marked with the entry 'NOT_AVAILABLE'

  2. if data[CO BORROWER NAME'] is not equal to 'NOT_AVAILABLE' and if any other value presents data['CO BORROWER_STATUS'] should be marked as 'AVAILABLE'

I have made for loop, and if condition for the same and iterated through the same. But it is taking more than 15 minutes to execute. Is there any other other easy way ?

for i in range(0,13961):

    if data['CO BORROWER NAME'][i] == 'NOT_AVAILABLE':

        data['CO BORROWER_STATUS'][i]='NOT_AVAILABLE'

    else:

        data['CO BORROWER_STATUS'][i]='AVAILABLE'

O/p expected:    
data['CO BORROWER_STATUS'] column should have either 'AVAILABLE'/'NOT 
AVAILABLE' based on the condition in column data['CO BORROWER NAME'] as 
mentioned earlier
desertnaut
  • 57,590
  • 26
  • 140
  • 166
Ayyasamy
  • 149
  • 1
  • 13
  • Those answer is not exactly relevant , it seems matching, but in run time basis, i am looking for some efficient code. please un-mark this from 'duplicate' – Ayyasamy Aug 08 '19 at 11:57

1 Answers1

1

Please try

import numpy as np
data['CO BORROWER_STATUS'] = np.select([data['a'] == 'NOT_AVAILABLE'],['NOT_AVAILABLE'], ['AVAILABLE'])
Neo
  • 627
  • 3
  • 7
  • Why suggest using numpy as he wants a solution with pandas – Sundeep Pidugu Aug 08 '19 at 11:46
  • 1
    Performance-wise, this is probably one of the best practice. See the accepted answer here https://stackoverflow.com/questions/57409229/pandas-if-else-conditions-on-multiple-columns/57409336#57409336 – Neo Aug 08 '19 at 11:48
  • 1
    `data.loc[data['CO BORROWER NAME'].eq('NOT_AVAILABLE'), 'CO BORROWER_STATUS'] = 'NOT_AVAILABLE'`; `data.loc[data['CO BORROWER NAME'].ne('NOT_AVAILABLE'), 'CO BORROWER_STATUS'] = 'AVAILABLE'` would be pandas only. It's not as efficient though – help-ukraine-now Aug 08 '19 at 11:51
  • 1
    this is awesome, Mr.Neofytos , this works and result in a fraction of seconds. Thanks !! – Ayyasamy Aug 08 '19 at 12:05