You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于DataFrame分组统计correct=True数量绘制折线图?

实现方案
  • 统计各number分组的正确记录数
    利用groupby按'number'分组后,直接对'correct'列求和(布尔值True会被视为1,False视为0,求和结果即为对应分组的正确记录数量),最后用reset_index()将分组索引转为普通列:

    correct_counts = df.groupby('number')['correct'].sum().reset_index()
    
  • 绘制折线图
    使用matplotlib快速生成折线图,设置x轴为'number',y轴为统计出的正确记录数,同时优化刻度显示保证0-12全部展示:

    import matplotlib.pyplot as plt
    
    plt.figure(figsize=(10, 6))
    plt.plot(correct_counts['number'], correct_counts['correct'], marker='o', linestyle='-', color='#1f77b4')
    plt.xlabel('number')
    plt.ylabel('正确记录数量')
    plt.title('各number分组下正确记录数量趋势')
    plt.xticks(range(0, 13))  # 强制显示0到12的所有刻度
    plt.grid(axis='y', linestyle='--', alpha=0.7)
    plt.show()
    

如果你的'correct'列不是布尔类型(比如存储为字符串'True'/'False'),需要先转换类型:

df['correct'] = df['correct'].astype(bool)

内容的提问来源于stack exchange,提问作者Arthur7920

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 17:10:34