【问题标题】:Obtain most recent value for based on index in a pandas dataframe根据熊猫数据框中的索引获取最新值
【发布时间】:2021-05-31 20:06:43
【问题描述】:

在每个会议日期,都会对未来 3 到 4 年的 GDP 增长做出新的预测。如果forecast_year 的 gdp 增长预测与上一个meeting_date 中的相似,则它不会在表中获得新条目。

是否有一种简单的方法可以为所有meeting_dates 添加这些缺失的forecast_year 条目,并引用截至最新meeting_date 提供的最新gdp_growth(%) 数字?

为了澄清,这里是输入表df_in

meeting_date forecast_year gdp_growth (%)
2007-11-20 2007 2.45
2007-11-20 2008 2.15
2007-11-20 2009 2.50
2007-11-20 2010 2.55
2008-02-20 2008 1.65
2008-02-20 2009 2.40
2008-02-20 2010 2.75
2008-05-21 2008 0.75
2008-05-21 2010 2.85
2008-07-16 2008 1.30
2008-07-16 2010 2.75
2008-11-19 2008 0.15
2008-11-19 2009 0.45
2008-11-19 2011 3.20

这是我需要输出的输出表的形状,df_out。需要添加的行以粗体显示。将df_in 转换为df_out 需要哪些pandas 操作?

meeting_date forecast_year GPD Growth (%)
2007-11-20 2007 2.45
2007-11-20 2008 2.15
2007-11-20 2009 2.50
2007-11-20 2010 2.55
2008-02-20 2008 1.65
2008-02-20 2009 2.40
2008-02-20 2010 2.75
2008-05-21 2008 0.75
2008-05-21 2009 2.40
2008-05-21 2010 2.85
2008-07-16 2008 1.30
2008-07-16 2009 2.40
2008-07-16 2010 2.75
2008-11-19 2008 0.15
2008-11-19 2009 0.45
2008-11-19 2010 2.75
2008-11-19 2011 3.20

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    通过pivot的另一种方式:

    k = df1.pivot(*df1).ffill().stack().reset_index(name = 'GPD Growth (%)')
    df = k[~(pd.to_datetime(k["meeting_date"]).dt.year.gt(k["forecast_year"]))]
    

    输出:

       meeting_date  forecast_year  GPD Growth (%)
    0    2007-11-20           2007            2.45
    1    2007-11-20           2008            2.15
    2    2007-11-20           2009            2.50
    3    2007-11-20           2010            2.55
    5    2008-02-20           2008            1.65
    6    2008-02-20           2009            2.40
    7    2008-02-20           2010            2.75
    9    2008-05-21           2008            0.75
    10   2008-05-21           2009            2.40
    11   2008-05-21           2010            2.85
    13   2008-07-16           2008            1.30
    14   2008-07-16           2009            2.40
    15   2008-07-16           2010            2.75
    17   2008-11-19           2008            0.15
    18   2008-11-19           2009            0.45
    19   2008-11-19           2010            2.75
    20   2008-11-19           2011            3.20
    

    【讨论】:

      【解决方案2】:

      试试:

      x = (
          df.set_index(["meeting_date", "forecast_year"])
          .unstack(level=1)
          .ffill()
          .stack()
          .reset_index()
      )
      # remove rows where meeting_date > forecast_year
      x = x[~(pd.to_datetime(x["meeting_date"]).dt.year > x["forecast_year"])]
      print(x)
      

      打印:

         meeting_date  forecast_year  gdp_growth (%)
      0    2007-11-20           2007            2.45
      1    2007-11-20           2008            2.15
      2    2007-11-20           2009            2.50
      3    2007-11-20           2010            2.55
      5    2008-02-20           2008            1.65
      6    2008-02-20           2009            2.40
      7    2008-02-20           2010            2.75
      9    2008-05-21           2008            0.75
      10   2008-05-21           2009            2.40
      11   2008-05-21           2010            2.85
      13   2008-07-16           2008            1.30
      14   2008-07-16           2009            2.40
      15   2008-07-16           2010            2.75
      17   2008-11-19           2008            0.15
      18   2008-11-19           2009            0.45
      19   2008-11-19           2010            2.75
      20   2008-11-19           2011            3.20
      

      编辑:删除 MultiIndex.from_product - 不需要

      【讨论】:

        猜你喜欢
        • 2018-08-04
        • 2021-01-19
        • 1970-01-01
        • 2021-06-05
        • 1970-01-01
        • 2021-07-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多