Python 我得到这个错误类型error:'<=';在';int';和';str';
我想把一个数值变量转换成一个分类变量。这是我正在使用的代码Python 我得到这个错误类型error:'<=';在';int';和';str';,python,pandas,Python,Pandas,我想把一个数值变量转换成一个分类变量。这是我正在使用的代码 def transformFunction(value): if value>=390 and value<=459 or value==785: category = 'Circulatory' elif value>=460 and value<=519 or value==786: category = 'Respiratory' else :
def transformFunction(value):
if value>=390 and value<=459 or value==785:
category = 'Circulatory'
elif value>=460 and value<=519 or value==786:
category = 'Respiratory'
else :
category = 'Other'
return category
for col in diag_list:
train[col] = train[col].apply(transformFunction)
def转换函数(值):
如果值>=390,值=460,值3列[col]=列[col]。应用(转换功能)
应用中的~\Anaconda3\lib\site packages\pandas\core\series.py(self、func、convert\u dtype、args、**kwds)
4040其他:
4041 values=self.astype(object.values)
->4042 mapped=lib.map\u expert(值,f,convert=convert\u数据类型)
4043
4044如果len(已映射)和isinstance(已映射[0],系列):
熊猫\\u libs\lib.pyx在熊猫中。_libs.lib.map\u infere()
在转换函数中(值)
6.
7 def转换功能(值):
---->8如果值>=390,值=460,值则该函数用于数字(int
)。您正在将其与文本(str
)一起使用
您不应该将transform函数应用于所有列,只应用于其中一列。摆脱循环:
for col in diag_list:
并访问要对其应用转换的一列
train["thecolumn"] = train["thecolumn"].apply(transformFunction)
了解您拥有的数据是的第2步。似乎您已经进入了步骤3(“数据准备”),而不知道数据是什么。将列作为数字类型传递:train[col].astype(int).apply(transformFunction)
或train[col].astype(float).apply(transformFunction)
。您的答案不完整-如果没有示例数据,我们必须猜测解决方案可能是什么。
train["thecolumn"] = train["thecolumn"].apply(transformFunction)