我想用这种方法从我的数据中创建分类变量:
cat.var condition
1 x > 10
2 x == 10
3 x < 10
我尝试使用来自的C() 方法patsy
,但它不起作用,我知道在 stata 中我必须使用下面的代码,但在搜索后我没有找到任何干净的方法来做到这一点pyhton
:
generate mpg3 = .
(74 missing values generated)
replace mpg3 = 1 if (mpg <= 18)
(27 real changes made)
replace mpg3 = 2 if (mpg >= 19) & (mpg <=23)
(24 real changes made)
replace mpg3 = 3 if (mpg >= 24) & (mpg <.)
(23 real changes made