我试图将列替换为基于条件的值(基本上,我试图在此列中创建索引类以进行多项逻辑回归)
我尝试使用DataFrame.loc,但总是遇到相同的错误
任何人都可以帮助我解决它吗?
data.loc[(data['Freq_Rech'] < 3), 'Freq_Rech'] = 1
data.loc[(data['Freq_Rech'] >= 3) & (data['Freq_Rech'] < 6), 'Freq_Rech'] = 3
data.loc[data['Freq_Rech'] >= 6 & data['Freq_Rech'] < 10, 'Freq_Rech'] = 6
data.loc[data['Freq_Rech'] >= 10 & data['Freq_Rech'] < 20, 'Freq_Rech'] = 10
data.loc[data['Freq_Rech'] >= 20, 'Freq_Rech'] = 20
错误是: 无法将类型为[bool]的标量的类型为[float64]的数组进行比较
答案 0 :(得分:0)
您快到了。只需确保在布尔条件周围有正确的语法即可:
data.loc[(data['Freq_Rech'] < 3), 'Freq_Rech'] = 1
data.loc[(data['Freq_Rech'] >= 3) & (data['Freq_Rech'] < 6), 'Freq_Rech'] = 3
data.loc[(data['Freq_Rech'] >= 6) & (data['Freq_Rech'] < 10), 'Freq_Rech'] = 6
data.loc[(data['Freq_Rech'] >= 10) & (data['Freq_Rech'] < 20), 'Freq_Rech'] = 10
data.loc[(data['Freq_Rech'] >= 20), 'Freq_Rech'] = 20