【问题标题】:Pandas TypeError: unsupported operand type(s) for /: 'str' and 'int'Pandas TypeError:/:'str'和'int'不支持的操作数类型
【发布时间】:2021-10-13 16:44:16
【问题描述】:

我正在分析学生的表现,我想计算数学成绩高于 80 的学生的百分比,我将分数从 string 转换为 int,但仍然出现错误。为什么?

    import pandas as pd 

    df = pd.read_csv('Data/StudentsPerformance.csv')
    df['math_score'] = df['math_score'].astype(int)
    filt = (df['math_score'] >= 80)
    higherthan80 = df.loc[filt]
    respond_count = df.gender.count()
    part = higherthan80 / respond_count

【问题讨论】:

  • 使用“分而治之”查找导致错误的行。
  • @DYZ 最后一行导致错误,它说不支持的操作数类型,但它们都是整数。
  • 您正在划分数据框中的所有列,而不仅仅是math_score 列。
  • 我不明白这应该如何计算任何东西的百分比。你为什么要除以每个性别的计数?

标签: python pandas dataframe data-science data-analysis


【解决方案1】:

现在,您正尝试将整个数据帧划分为 respond_count。尽量只取 math_score 字段。

higherthan80 = df.loc[df['math_score'] >= 80, "math_score"]

【讨论】:

    猜你喜欢
    • 2016-04-15
    • 2012-11-29
    • 2012-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多