[英]Convert numerical data to categorical in Python
I have a pandas dataframe with the column fert_Rate for fertility rate.我有一个 Pandas 数据框,列 fert_Rate 表示生育率。 I want to have a new column with these values as categorical instead of numerical.
我想有一个新列,这些值是分类而不是数字。 Instead of 1.0, 2.5, 4.0 I want something like (low, medium, high).
我想要的不是 1.0、2.5、4.0,而是(低、中、高)。 In RI would have written something like this:
在 RI 中会这样写:
attach(mydata)
mydata$fertcat[fert_Rate > 3.5] <- "High"
mydata$fertcat[fert_Rate > 2 & fert_Rate <= 3.5] <- "Medium"
mydata$fertcat[fert_Rate <= 2] <- "Low"
detach(mydata)
Is there a similar way to do it in python or should I just loop over the column to create?有没有类似的方法可以在 python 中做到这一点,还是我应该循环遍历列来创建?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.