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

Python数据科学作业:补全新冠数据30-40岁组误差棒图函数

补全30-40岁年龄组新冠阳性结果时间趋势误差棒图函数

需求:绘制30-40岁年龄组新冠阳性结果相对频率随时间变化的误差棒图,包含各点上下1个标准差,补全以下函数的关键代码段:

def create_bar_plot_for_derek(covid19_data):
  # first we subset the data by the appropriate age bracket and do a bit of cleaning
  prof_age_data = covid19_data[covid19_data.age_range=="30-40"]
  prof_age_data=prof_age_data.replace(to_replace='25.02.2020 - 26.02.2020',value='25.02.2020')

  # and we convert the column to a date-time
  prof_age_data['date_confirmation']=pd.to_datetime(prof_age_data['date_confirmation'],dayfirst=True)

  outcomes_over_time =  **# Problem 6) fill in here**

  outcomes_over_time = outcomes_over_time.dropna() # we should drop the rows with missing values

  x =  **# Problem 6) fill in here**
  y =  **# Problem 6) fill in here**
  error =  **# Problem 6) fill in here**

  fig, ax = plt.subplots(figsize=(20, 10))
  ax.errorbar(x, y, yerr=error, fmt='-o')
  plt.ylabel('Relative Frequency', fontsize=14)
  plt.xlabel('Date', fontsize=14)
  return x, y, error

补全后的完整函数

import pandas as pd
import matplotlib.pyplot as plt

def create_bar_plot_for_derek(covid19_data):
    # first we subset the data by the appropriate age bracket and do a bit of cleaning
    prof_age_data = covid19_data[covid19_data.age_range=="30-40"]
    prof_age_data=prof_age_data.replace(to_replace='25.02.2020 - 26.02.2020',value='25.02.2020')

    # and we convert the column to a date-time
    prof_age_data['date_confirmation']=pd.to_datetime(prof_age_data['date_confirmation'],dayfirst=True)

    # Problem 6) fill in here: 按日期分组计算阳性相对频率和标准差
    outcomes_over_time = prof_age_data.groupby('date_confirmation').apply(
        lambda group: pd.Series({
            'relative_freq': (group['outcome'] == 'positive').mean(),
            'std_dev': (group['outcome'] == 'positive').std()
        })
    ).reset_index()

    outcomes_over_time = outcomes_over_time.dropna() # we should drop the rows with missing values

    # Problem 6) fill in here: 提取绘图所需的X/Y轴及误差数据
    x = outcomes_over_time['date_confirmation']
    y = outcomes_over_time['relative_freq']
    error = outcomes_over_time['std_dev']

    fig, ax = plt.subplots(figsize=(20, 10))
    ax.errorbar(x, y, yerr=error, fmt='-o')
    plt.ylabel('Relative Frequency', fontsize=14)
    plt.xlabel('Date', fontsize=14)
    return x, y, error

关键补全逻辑说明

  • outcomes_over_time 代码段:
    按date_confirmation分组,对每组计算:

    • 阳性结果相对频率:通过(group['outcome'] == 'positive').mean()得到(阳性样本数/组内总样本数)
    • 标准差:通过(group['outcome'] == 'positive').std()得到组内阳性结果的标准差
      注意:如果你的数据中记录检测结果的列名不是outcome,阳性标记不是'positive',请替换为实际的列名和取值
  • x/y/error 代码段:
    直接从分组统计后的outcomes_over_time数据框中提取对应字段:

    • x为日期序列,作为绘图的X轴
    • y为每日阳性相对频率,作为绘图的Y轴
    • error为每日阳性结果的标准差,用于绘制误差棒

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 02:52:53