pandas 我注意到在修复类型错误后,我的表中有额外的和不必要的数据无法将系列转换为类float

bkhjykvo  于 2023-08-01  发布在  其他
关注(0)|答案(1)|浏览(98)

例如,当我获得学生总数时,我注意到的第一件事是,当我运行代码的单元格时,

# Get the total number of students.
student_count = school_data_complete_df.count()
student_count

字符串
我得到了以下预期的结果:enter image description here
即使采样输出也提供相同的结果,所以我是正确的。但是,当我在表上运行 Dataframe 时,我会得到以下结果:enter image description here
这就是正确的采样输出应该是什么:enter image description here
我注意到类似的异常现象,在后面的部分时,运行我的通过百分比的数学和阅读的学生。首先,我决定数学和阅读评估测试的及格分数:

passing_math = school_data_complete_df["math_score"] >= 70
passing_reading = school_data_complete_df["reading_score"] >= 70


我得到的输出与预期的输出略有不同:enter image description here
我刚才注意到了
下面是正确的输出:enter image description here
我的其余代码运行正常

# Get all the students that are passing reading in a new DataFrame.
passing_reading = school_data_complete_df[school_data_complete_df["reading_score"] >= 70]

# Calculate the number of students passing math.
passing_math_count = passing_math["student_name"].count()

# Calculate the number of students passing reading.
passing_reading_count = passing_reading["student_name"].count()

print(passing_math_count)
print(passing_reading_count)


直到我犯了这个错误:

# Calculate the percent that passed math.
passing_math_percentage = passing_math_count / float(student_count) * 100

# Calculate the percent that passed reading.
passing_reading_percentage = passing_reading_count / float(student_count) * 100


这个代码单元后面的信息如下所示:
enter image description here
然而,当我试图运行代码时,我收到一个类型错误,无法将系列转换为类float。我通过编辑我的代码单元来缓解这个问题,看起来像这样:

# Calculate the percent that passed math.
passing_math_percentage = passing_math_count / student_count.astype("float") * 100

# Calculate the percent that passed reading.
passing_reading_percentage = passing_reading_count / student_count.astype("float") * 100


现在我没有收到错误,但这是创建一个地区摘要数据框架后我的整个表现在的样子:

# Adding a list of values with keys to create a new DataFrame.
district_summary_df = pd.DataFrame(
          [{"Total Schools": school_count,
          "Total Students": student_count,
          "Total Budget": total_budget,
          "Average Math Score": average_math_score,
          "Average Reading Score": average_reading_score,
          "% Passing Math": passing_math_percentage,
         "% Passing Reading": passing_reading_percentage,
        "% Overall Passing": overall_passing_percentage}])
district_summary_df


enter image description here
我只希望百分比值出现在我的表中,而一个数字出现在学生总数列中。正确的采样输出如下所示:enter image description here

20jt8wwn

20jt8wwn1#

从第二张图像中,

学生总数列看起来不正确

相关问题