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

如何使用Python Pandas将月度数据转换为年度均值数据?

按年份计算移动用户订阅量均值的实现方法

操作步骤(基于Pandas)

  • 转换日期格式:先把month列转为日期类型,方便后续提取年份:

    import pandas as pd
    
    # 假设你的数据已经加载到df变量里
    df['month'] = pd.to_datetime(df['month'])
    
  • 提取年份字段:从日期列里拆分出年份,新增year列:

    df['year'] = df['month'].dt.year
    
  • 分组计算年度均值:按年份分组,对订阅量列求均值,结果自动按年份排序:

    yearly_avg_data = df.groupby('year')['total_mobile_subscription'].mean().reset_index()
    
  • (可选)保留整数格式:如果需要把均值转为整数(订阅量通常是整数),可以加这一步:

    yearly_avg_data['total_mobile_subscription'] = yearly_avg_data['total_mobile_subscription'].round().astype(int)
    
  • 查看最终结果:

    print(yearly_avg_data)
    

内容的提问来源于stack exchange,提问作者Lim Ze Kai

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 08:40:48