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

使用Pandas进行累计求和时,如何填充年份缺口并赋值?

数据集填充需求与实现方案

原始数据集(小样本)

City    Year    Votes
Detroit 1964    23
Detroit 1977    61
Detroit 1978    89
Detroit 1986    116
Detroit 1993    144
Baltimore   1964    42
Baltimore   1965    91
Baltimore   1966    161
Baltimore   1967    219
Baltimore   1968    263
Baltimore   1969    312
Baltimore   1970    346
Baltimore   1978    375
Baltimore   1980    415
Baltimore   1981    449
Baltimore   1995    484
Baltimore   1996    529
Baltimore   1997    578
Baltimore   1998    619
Baltimore   1999    660
Baltimore   2000    713
Baltimore   2001    757
Baltimore   2002    807
Baltimore   2003    852
Baltimore   2004    884
Boston  1968    47
Boston  1969    101
Boston  1970    123
Boston  2007    157
Phoenix 1971    41
Phoenix 1972    41
Phoenix 1979    76
Phoenix 1981    112
Phoenix 1982    154
Phoenix 1983    197
Phoenix 1984    242
Phoenix 1985    279
Phoenix 1997    319
Phoenix 1998    351
Phoenix 2000    381
Phoenix 2003    417
Phoenix 2005    457
Phoenix 2006    494
Phoenix 2007    536
Phoenix 2008    570
Phoenix 2009    598
Phoenix 2021    633
Phoenix 2022    661

填充规则

需要将年份范围覆盖1950至2023,为每个城市填充缺失年份的投票数,规则如下:

  • 若城市在1950年有投票数据,直接使用该值
  • 若城市在1950年无投票数据,以0作为起始值
  • 所有缺失年份均采用前一年的投票值填充

预期结果示例(Detroit填充后数据)

City    Year    Votes
Detroit 1950    0
Detroit 1951    0
Detroit 1952    0
Detroit 1953    0
Detroit 1954    0
Detroit 1955    0
Detroit 1956    0
Detroit 1957    0
Detroit 1958    0
Detroit 1959    0
Detroit 1960    0
Detroit 1961    0
Detroit 1962    0
Detroit 1963    0
Detroit 1964    23
Detroit 1965    23
Detroit 1966    23
Detroit 1967    23
Detroit 1968    23
Detroit 1969    23
Detroit 1970    23
Detroit 1971    23
Detroit 1972    23
Detroit 1973    23
Detroit 1974    23
Detroit 1975    23
Detroit 1976    23
Detroit 1977    61
Detroit 1978    89
Detroit 1979    89
Detroit 1980    89
Detroit 1981    89
Detroit 1982    89
Detroit 1983    89
Detroit 1984    89
Detroit 1985    89
Detroit 1986    116
Detroit 1987    116
Detroit 1988    116
Detroit 1989    116
Detroit 1990    116
Detroit 1991    116
Detroit 1992    116
Detroit 1993    144
Detroit 1994    144
Detroit 1995    144
Detroit 1996    144
Detroit 1997    144
Detroit 1998    144
Detroit 1999    144
Detroit 2000    144
Detroit 2001    144
Detroit 2002    144
Detroit 2003    144
Detroit 2004    144
Detroit 2005    144
Detroit 2006    144
Detroit 2007    144
Detroit 2008    144
Detroit 2009    144
Detroit 2010    144
Detroit 2011    144
Detroit 2012    144
Detroit 2013    144
Detroit 2014    144
Detroit 2015    144
Detroit 2016    144
Detroit 2017    144
Detroit 2018    144
Detroit 2019    144
Detroit 2020    144
Detroit 2021    144
Detroit 2022    144
Detroit 2023    144

Python pandas实现代码

import pandas as pd

# 构造原始数据集(实际使用时可替换为pd.read_csv读取文件)
raw_data = pd.DataFrame({
    'City': ['Detroit', 'Detroit', 'Detroit', 'Detroit', 'Detroit',
             'Baltimore', 'Baltimore', 'Baltimore', 'Baltimore', 'Baltimore',
             'Baltimore', 'Baltimore', 'Baltimore', 'Baltimore', 'Baltimore',
             'Baltimore', 'Baltimore', 'Baltimore', 'Baltimore', 'Baltimore',
             'Baltimore', 'Baltimore', 'Baltimore', 'Baltimore', 'Baltimore',
             'Boston', 'Boston', 'Boston', 'Boston',
             'Phoenix', 'Phoenix', 'Phoenix', 'Phoenix', 'Phoenix',
             'Phoenix', 'Phoenix', 'Phoenix', 'Phoenix', 'Phoenix',
             'Phoenix', 'Phoenix', 'Phoenix', 'Phoenix', 'Phoenix',
             'Phoenix', 'Phoenix', 'Phoenix', 'Phoenix'],
    'Year': [1964, 1977, 1978, 1986, 1993,
             1964, 1965, 1966, 1967, 1968, 1969, 1970, 1978, 1980, 1981,
             1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
             1968, 1969, 1970, 2007,
             1971, 1972, 1979, 1981, 1982, 1983, 1984, 1985, 1997, 1998,
             2000, 2003, 2005, 2006, 2007, 2008, 2009, 2021, 2022],
    'Votes': [23, 61, 89, 116, 144,
              42, 91, 161, 219, 263, 312, 346, 375, 415, 449,
              484, 529, 578, 619, 660, 713, 757, 807, 852, 884,
              47, 101, 123, 157,
              41, 41, 76, 112, 154, 197, 242, 279, 319, 351,
              381, 417, 457, 494, 536, 570, 598, 633, 661]
})

# 生成1950-2023的完整年份序列
full_year_range = pd.DataFrame({'Year': range(1950, 2024)})
unique_cities = raw_data['City'].unique()
filled_data = pd.DataFrame()

# 逐个城市处理填充逻辑
for city in unique_cities:
    city_raw = raw_data[raw_data['City'] == city].copy()
    # 合并完整年份与城市原始数据
    merged = pd.merge(full_year_range, city_raw, on='Year', how='left')
    merged['City'] = city
    # 设置1950年的起始值
    if pd.isna(merged.loc[merged['Year'] == 1950, 'Votes'].iloc[0]):
        merged.loc[merged['Year'] == 1950, 'Votes'] = 0
    # 向前填充缺失值
    merged['Votes'] = merged['Votes'].ffill()
    # 合并到总数据集
    filled_data = pd.concat([filled_data, merged], ignore_index=True)

# 按城市和年份排序
filled_data = filled_data.sort_values(['City', 'Year']).reset_index(drop=True)

# 输出结果或保存为文件
print(filled_data.to_string(index=False))
# filled_data.to_csv('filled_votes_data.csv', index=False)

代码说明

  1. 生成完整年份序列:创建覆盖1950到2023的所有年份,确保每个城市的时间范围完整
  2. 单城市数据合并:遍历每个城市,将其原始数据与完整年份序列合并,生成包含缺失年份的数据集
  3. 起始值设置:检查1950年是否有数据,无数据则设为0
  4. 向前填充:使用ffill()方法自动将缺失年份的投票数填充为前一年的有效值
  5. 排序输出:按城市和年份排序后得到最终结果,可直接打印或保存为CSV文件

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 23:45:28