Numpy where condition when met must modify the original value, if not original value must remain

Viewed 73

I have the following dataframe:

Policy_id Value 
     A           xyz
     B           abc
     A           pqr
     C           lmn

And I want to use np.where() such that whenever the policy_id is equal to A the corresponding value must be appended with a *.

Policy_id    Value 
     A           xyz*
     B           abc
     A           pqr*
     C           lmn

How do I achieve this?

2 Answers

Try

df['new'] = np.where(df['Policy_id'].eq('A'),df['Value']+'*',df['Value'])

If you want to modify your dataframe in place and use np.where:

df['Value'] += np.where(df['Policy_id'].eq('A'), '*', '')

output:

  Policy_id Value
0         A  xyz*
1         B   abc
2         A  pqr*
3         C   lmn
Related