对于如何在python的数据框架中跨列应用函数,我有一些疑问:当我试图使用remove_outlier_IQR ()在数据frame中的所有列中应用此delete_outlier_IQR函数时,会出现以下不受支持的操作数错误:
def remove_outlier_IQR(feature):
Q1 = df[feature].quantile(0.25)
Q3 = df[feature].quantile(0.75)
IQR = Q3 - Q1
upbound = Q3 + 6 * IQR
downbound = Q1 - 6 * IQR
num_outlier = len(df[((df[feature] < downbound)|(df[feature] > upbound))][feature])
percent_outlier = num_outlier / len(df[feature])
# print(percent_outlier)
if(percent_outlier > 0.1):
index_outlier = list(df[((df[feature] < downbound)|(df[feature] > upbound))][feature].index)
# print(index_outlier)
print('outlier > 10%')
else:
index_outlier = list(df[((df[feature] < downbound)|(df[feature] > upbound))][feature].index)
# print(index_outlier)
print('outlier < 10%')
def delete_outlier_IQR():
for col in df.columns:
remove_outlier_IQR(col)
delete_outlier_IQR()这就是我的错误:
TypeError: unsupported operand type(s) for -: 'str' and 'str'发布于 2022-06-30 08:56:40
似乎您正在尝试使用Strings进行计算。首先将它们转换为整数,这将解决问题。
示例:
int(to_be_converted_string) - int(other_string)https://stackoverflow.com/questions/72812876
复制相似问题