python - 长格式的 Pandas 数据框缺少月份

标签 python pandas date

我找不到在长格式数据框中添加月份的方法。 例如,对于每个国家,每年我都希望有 12 个值,每月一次。 total_points 的值(将为 NaN)将与上一个相同。 这意味着我的列月份应该是每个国家和每年的范围(1,13)。

country_full    year    month        rank   rank_date   total_points    
Zimbabwe       2021        8         108    2021-08-12  1171.88
Zimbabwe       2021        10        108    2021-09-12  1171.88
Zimbabwe       2022        01        108    2022-01-12  1171.88
Germany        1994        01        10     1994-01-10  1171.88
Germany        1994        02        10     1994-02-09  1327.8
Germany        1994        04        10     1994-04-07  1459.9

期望的输出是:

country_full    year    month        rank   rank_date   total_points    
    Zimbabwe       2021        8         108    2021-08-12  1171.88
    Zimbabwe       2021        9         108    2021-08-12  1171.88
    Zimbabwe       2021        10        108    2021-10-12  1171.88
    Zimbabwe       2021        11        108    2021-11-12  1171.88
    Zimbabwe       2021        12        108    2021-12-12  1171.88
    Zimbabwe       2022        01        108    2022-01-12  1171.88
    Germany        1994        01        10     1994-01-10  1171.88
    Germany        1994        02        10     1994-02-10  1327.8
    Germany        1994        03        10     1994-03-09  1459.9
    Germany        1994        04        10     1994-03-07  1459.9

对于每个国家和每年有 12 个月。总分列的缺失值将是上个月的值。

知道我们如何用 pandas 做到这一点吗?

最佳答案

假设所有 rank_date 都在 12 那天,您可以使用 MS 频率的 reindex 然后使用 pd.offsets 添加天数:

new_indexes = ( pd.date_range(start=f"{df['year'].min()}", 
  end=f"{df['year'].max() + 1}", freq='MS', inclusive='left') + pd.offsets.Day(11) )

# forward and backward fills and reindexing the columns to the actual format
new_df = ( df.set_index('rank_date').reindex(new_indexes).ffill().bfill().
      reset_index().rename({'index':'rank_date'}, axis=1).reindex(df.columns, axis=1) )

# we need to update year and month columns                                                                                                                           
new_df['year'] = new_df['rank_date'].dt.year
new_df['month'] = new_df['rank_date'].dt.month

输出:

country_full    year    month   rank    rank_date   total_points
0   Zimbabwe    2021    1   108.0   2021-01-12  1171.88
1   Zimbabwe    2021    2   108.0   2021-02-12  1171.88
2   Zimbabwe    2021    3   108.0   2021-03-12  1171.88
3   Zimbabwe    2021    4   108.0   2021-04-12  1171.88
4   Zimbabwe    2021    5   108.0   2021-05-12  1171.88
5   Zimbabwe    2021    6   108.0   2021-06-12  1171.88
6   Zimbabwe    2021    7   108.0   2021-07-12  1171.88
7   Zimbabwe    2021    8   108.0   2021-08-12  1171.88
8   Zimbabwe    2021    9   108.0   2021-09-12  1171.88
9   Zimbabwe    2021    10  108.0   2021-10-12  1171.88
10  Zimbabwe    2021    11  108.0   2021-11-12  1171.88
11  Zimbabwe    2021    12  108.0   2021-12-12  1171.88
12  Zimbabwe    2022    1   108.0   2022-01-12  1171.88
13  Zimbabwe    2022    2   108.0   2022-02-12  1171.88
14  Zimbabwe    2022    3   108.0   2022-03-12  1171.88
15  Zimbabwe    2022    4   108.0   2022-04-12  1171.88
16  Zimbabwe    2022    5   108.0   2022-05-12  1171.88
17  Zimbabwe    2022    6   108.0   2022-06-12  1171.88
18  Zimbabwe    2022    7   108.0   2022-07-12  1171.88
19  Zimbabwe    2022    8   108.0   2022-08-12  1171.88
20  Zimbabwe    2022    9   108.0   2022-09-12  1171.88
21  Zimbabwe    2022    10  108.0   2022-10-12  1171.88
22  Zimbabwe    2022    11  108.0   2022-11-12  1171.88
23  Zimbabwe    2022    12  108.0   2022-12-12  1171.88

关于python - 长格式的 Pandas 数据框缺少月份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74172508/

相关文章:

python - 在 matplotlib 注释中添加一行

python - 分组转移 Pandas

python - 如何将一列中的行值与组内不同列中的所有其他行进行比较?

python - 如何为另一列中的特定值选择 pandas 列中的值计数?

ruby-on-rails - 如何使用可变日期在 Rails 中搜索日期范围

python - 验证 sqlalchemy session

python - 如何创建一个包含方程的函数并将其应用于不同的数据帧?

python - 如何在数据透视表上添加总和和计数

r - 如何申报每周数据的时间序列?

javascript - 从特定日期获取天数