Pandas TypeError:&;的操作数类型不受支持:';str';和';布尔';使用np.select
尝试将np.select与以下代码一起使用时,我遇到了一个Pandas TypeError:&;的操作数类型不受支持:';str';和';布尔';使用np.select,pandas,numpy,Pandas,Numpy,尝试将np.select与以下代码一起使用时,我遇到了一个类型错误:&:'str'和'bool'的操作数类型不受支持 condition = [(tt_df.loc[tt_df['RATING_BASE_AMT'] <=1000]) ,(tt_df.loc[tt_df['RATING_BASE_AMT'] > 1000] & tt_df.loc[tt_df['RATING_BASE_AMT'] <=2000]), ,(tt_df.loc[tt_df['RATING_BA
类型错误:&:'str'和'bool'的操作数类型不受支持
condition = [(tt_df.loc[tt_df['RATING_BASE_AMT'] <=1000])
,(tt_df.loc[tt_df['RATING_BASE_AMT'] > 1000] & tt_df.loc[tt_df['RATING_BASE_AMT'] <=2000]),
,(tt_df.loc[tt_df['RATING_BASE_AMT'] > 2000])]
replace = [1000, 2000, 3000]
df['model_value'] = np.select(condition, replace, default = 1)
condition=[(tt_-df.loc[tt_-df['RATING_-BASE-AMT']1000]和tt_-df.loc[tt_-df['RATING_-BASE-AMT']2000])]
替换=[100020003000]
df['model_value']=np.select(条件,替换,默认值=1)
通过以下方式创建布尔掩码列表和过滤数据帧列表:
将值转换为数字:
tt_df['RATING_BASE_AMT'] = tt_df['RATING_BASE_AMT'].astype(float)
或者,如果可能,列中的一些非数值:
tt_df['RATING_BASE_AMT'] = pd.to_numeric(tt_df['RATING_BASE_AMT'], errors='coerce')
谢谢你@jezrael。仍然得到相同的错误。“他们现在64岁了。”乔丹-哎呀,问题出在别的地方,编辑回答。
tt_df['RATING_BASE_AMT'] = pd.to_numeric(tt_df['RATING_BASE_AMT'], errors='coerce')
replace = [1000, 2000, 3000]
condition = [(df['RATING_BASE_AMT'] <=1000),
(df['RATING_BASE_AMT'] > 1000) & (df['RATING_BASE_AMT'] <=2000),
(df['RATING_BASE_AMT'] > 2000)]
df['model_value'] = np.select(condition, replace, default = 1)
df['model_value1'] = pd.cut(df['RATING_BASE_AMT'],
bins=[-np.inf, 1000, 2000, np.inf],
labels=[1000, 2000, 3000])
print (df)
RATING_BASE_AMT model_value model_value1
0 4 1000 1000
1 1000 1000 1000
2 999 1000 1000
3 1001 2000 2000
4 2000 2000 2000
5 2001 3000 3000