首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不支持的操作数类型str和str

不支持的操作数类型str和str
EN

Stack Overflow用户
提问于 2022-06-30 08:49:57
回答 1查看 56关注 0票数 -1

对于如何在python的数据框架中跨列应用函数,我有一些疑问:当我试图使用remove_outlier_IQR ()在数据frame中的所有列中应用此delete_outlier_IQR函数时,会出现以下不受支持的操作数错误:

代码语言:javascript
复制
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()

这就是我的错误:

代码语言:javascript
复制
TypeError: unsupported operand type(s) for -: 'str' and 'str'
EN

回答 1

Stack Overflow用户

发布于 2022-06-30 08:56:40

似乎您正在尝试使用Strings进行计算。首先将它们转换为整数,这将解决问题。

示例:

代码语言:javascript
复制
int(to_be_converted_string) - int(other_string)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72812876

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档