Peter Lucas
Peter Lucas

Reputation: 1991

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?

Upvotes: 1

Views: 73

Answers (2)

Neeraj Sharma
Neeraj Sharma

Reputation: 194

df <- structure(list(originating_system_id = c("BBT","BBT","BBT"), ticker = c("T 2 3/4 02/15/28","T 2 1/4 11/15/27","T 2 1/4 11/15/29"), cust_cdr_display_name = c(0, 0, 4),cust_username = c("BOB","BOB","BOB")), .Names = c("originating_system_id","ticker", "cust_cdr_display_name","cust_username"), row.names = c(NA, -3L), class = "data.frame")

Print df it will look as below. df

originating_system_id ticker cust_cdr_display_name cust_username

1 BBT T 2 3/4 02/15/28 0 BOB

2 BBT T 2 1/4 11/15/27 0 BOB

3 BBT T 2 1/4 11/15/29 4 BOB

It will check all indexes where cust_cdr_display_name is 0 and take values of cust_username and replace for same indexes.

df$cust_cdr_display_name[df$cust_cdr_display_name == 0] <- df$cust_username[df$cust_cdr_display_name == 0]

Output :

originating_system_id ticker cust_cdr_display_name cust_username

1 BBT T 2 3/4 02/15/28 BOB BOB

2 BBT T 2 1/4 11/15/27 BOB BOB

3 BBT T 2 1/4 11/15/29 4 BOB

Upvotes: 0

jezrael
jezrael

Reputation: 862491

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

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

Upvotes: 1

Related Questions