Ayyasamy
Ayyasamy

Reputation: 159

replacing a value based on value in other column, and leave as it is if condition not met

I am having a data-frame called data. Using np.select() I am replacing a value in column data['CO BORROWER_STATUS'] based on one condition given in column [data['CO BORROWER NAME']

  1. If [data['CO BORROWER NAME'] == 'NOT_AVAILABLE' I will change data['CO BORROWER_STATUS']= 'NOT_AVAILABLE'

2.If not there should be no change in the column data['CO BORROWER_STATUS']

I used np.select() method as shown below, but this replaces the entries to 0 if the condition not met. Is there any other changes We can do with the code to leave the default value without any change if the condition not met.

I need the answer using np.select() only or any other efficient methods, not others which will be not efficient, any ideas please ?

data['CO BORROWER_STATUS'] = np.select([data['CO BORROWER NAME'] == 'NOT_AVAILABLE'],['NOT_AVAILABLE'])

o/p-->:    
I am not passing the else argument here, hence the values are replaced 
with 0 in default for data['CO BORROWER_STATUS'] if condition fails.

Can I have no change if condition not met.

Upvotes: 1

Views: 96

Answers (1)

jezrael
jezrael

Reputation: 863531

If want use numpy.select set last parameter default to original values:

data['CO BORROWER_STATUS'] = np.select([data['CO BORROWER NAME'] == 'NOT_AVAILABLE'],
                                       ['NOT_AVAILABLE'], 
                                       default=data['CO BORROWER NAME'])

Because only one condition, better is numpy.where here:

data['CO BORROWER_STATUS'] = np.where(data['CO BORROWER NAME'] == 'NOT_AVAILABLE',
                                      'NOT_AVAILABLE', 
                                      data['CO BORROWER NAME'])

Upvotes: 3

Related Questions