Create Column with ELIF in Pandas

前端 未结 4 950
轻奢々
轻奢々 2020-11-29 01:50

Question

I am having trouble figuring out how to create new DataFrame column based on the values in two other columns. I need to use if/elif/else l

4条回答
  •  生来不讨喜
    2020-11-29 02:03

    Adding to np.where solution :

    df['col1']= np.where(df['col'] < 3, 1,np.where( (df['col'] >3 )& (df['col'] <5),2,3))
    

    Overall Logic is :

    np.where(Condition, 'true block','false block'). 
    

    With each true/false block can in turn again be nested.

    Also, Notice the & for ANDing! (not 'and')

提交回复
热议问题