compute columns based on multiple conditions

后端 未结 2 1700
独厮守ぢ
独厮守ぢ 2021-01-21 14:37

I was reading a blog for conditaion based new computations where new col \'category\' is inserted.

data = {\'name\': [\'Jason\', \'Molly\', \'Tina\', \'Jake\', \         


        
2条回答
  •  不知归路
    2021-01-21 15:08

    You can using pd.cut (BTW , 40 is not old man :-()

    pd.cut(df.age,bins=[0,20,39,np.inf],labels=['kid','young','old'])
    Out[179]: 
    0      old
    1      old
    2    young
    3    young
    4      old
    Name: age, dtype: category
    Categories (3, object): [kid < young < old]
    

提交回复
热议问题