Search code examples
pandasdataframecriteria

Update a columns string value based on multiple criteria in other columns


Looking to update any zero values in field cust_cdr_display_name with "BOC" if the cust_username is 'BOB'

originating_system_id   ticker         cust_cdr_display_name    cust_username
BBT                     T 2 3/4 02/15/28    0                    BOB
BBT                     T 2 1/4 11/15/27    0                    BOB


originating_system_id   ticker         cust_cdr_display_name    cust_username
BBT                     T 2 3/4 02/15/28    BOC                  BOB
BBT                     T 2 1/4 11/15/27    BOC                  BOB

Code:

mask = df[(   
            df['cust_cdr_display_name'] == 0
          ) 
        & 
          (
            df['cust_username'] == 'BOB'
          )]      
df.loc[mask, 'cust_cdr_display_name'] = 'BOC' 

I'm getting the error:

cannot copy sequence with size 40 to array axis with dimension 2

How to make the mask criteria accept multiple criteria?


Solution

  • You are close, need omit df[] in chained boolean masks:

    mask = (   
            df['cust_cdr_display_name'] == 0
           ) 
            & 
           (
            df['cust_username'] == 'BOB'
           )