1

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?

2 Answers 2

1

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

mask = (   
        df['cust_cdr_display_name'] == 0
       ) 
        & 
       (
        df['cust_username'] == 'BOB'
       )      
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

it is pandas, not r

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.