根据其他列中的多个条件更新列字符串值

时间:2018-04-10 05:50:25

标签: pandas dataframe criteria

如果cust_username为'BOB',则希望更新字段cust_cdr_display_name中的任何零值和“BOC”

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

代码:

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

我收到了错误:

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

如何使遮罩标准接受多个标准?

2 个答案:

答案 0 :(得分:1)

你很接近,需要在链式布尔掩码中省略df[]

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

答案 1 :(得分:0)

df< - structure(list(originating_system_id = c(" BBT"," BBT"," BBT"),ticker = c(&#34 ; 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")

打印,如下所示。 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

它将检查cust_cdr_display_name为0的所有索引,并获取cust_username的值并替换相同的索引。

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

输出:

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