根据python中的另外两列为列分配值

时间:2017-10-18 11:12:41

标签: python

我有两列转发,并提到像这样

retweet              mention

RT @CritCareMed:    
                     @CellCellPress
RT @CritCareMed:     @mother
RT @gvwilson:           
RT @sciencemagazine:        
RT @MHendr1cks:      @nucAmbiguous
                     @air

我想要一个新列,基于它是转推还是提及,如果它是一个提及其他分配R,则在新行中指定M.如果提及和转推都存在,则该行应具有值M,R。所以最终的结果应该是

 retweet                mention             Type

RT @CritCareMed:                              R
                        @CellCellPress        M
RT @CritCareMed:        @mother               R,M
RT @gvwilson:                                 R
RT @sciencemagazine:                          R
RT @MHendr1cks:         @nucAmbiguous         R,M
                        @air                  M

我现在正在做的事情就像是

df = df.assign(Type=np.where(df.retweet.isnull(), 'M','R'))

但它给了我结果

             retweet             mention        Type  
      RT @CritCareMed:             NaN           R  
                 NaN       @CellCellPress        M  
       RT @CritCareMed:         @mother          M  
          RT @gvwilson:             NaN          R  
   RT @sciencemagazine:             NaN          R  
        RT @MHendr1cks:   @nucAmbiguous          M  
                  NaN           @air             M 

其中第3行和第6行应该具有类型R,M但它只是给我M(从代码中可以预期)。如何修改代码以获得上述结果?

1 个答案:

答案 0 :(得分:0)

添加另一个条件以检查另一列:

df = df.assign(Type=np.where(df.retweet.isnull(), 'M',
                    np.where(df.mention.isnull(), 'R','R, M')))
print (df)
                retweet         mention  Type
0      RT @CritCareMed:             NaN     R
1                   NaN  @CellCellPress     M
2      RT @CritCareMed:         @mother  R, M
3         RT @gvwilson:             NaN     R
4  RT @sciencemagazine:             NaN     R
5       RT @MHendr1cks:   @nucAmbiguous  R, M
6                   NaN            @air     M