【问题标题】:Value calculation based on date in DataFrame in Python Pandas?Python Pandas中基于DataFrame中日期的值计算?
【发布时间】:2021-03-27 01:29:26
【问题描述】:

我有如下客户协议的 DataFrame:

rng = pd.date_range('2020-12-01', periods=5, freq='D')
df = pd.DataFrame({ "ID" : ["1", "2", "1", "2", "2"],
                   "value" : [100, 200, 300, 400, 500],
                   "status" : ["active", "finished", "active", "finished", "active"],
                   "Date": rng})

我需要根据上面的df计算创建新的DataFrame:

  1. New1 = 状态为“活动”的最后一个协议的值
  2. New2 = 状态为“已完成”的最后一个协议的值

为了更精确,我需要创建 df,如下所示:

【问题讨论】:

    标签: python pandas dataframe date


    【解决方案1】:

    在两列中使用DataFrame.sort_valuesDataFrame.pivot_table 以及聚合函数last

    df = (df.sort_values(['ID','Date'])
            .pivot_table(index='ID', columns='status', values='value', aggfunc='last')
            .rename(columns={'active':'New1','finished':'New2'})[['New1','New2']]
            .reset_index()
            .rename_axis(None,axis=1)
           )
    print (df)
      ID   New1   New2
    0  1  300.0    NaN
    1  2  500.0  400.0
    

    如果按组对日期时间进行排序,则解决方案更简单:

    df = (df.pivot_table(index='ID', columns='status', values='value', aggfunc='last')
            .rename(columns={'active':'New1','finished':'New2'})[['New1','New2']]
            .reset_index()
            .rename_axis(None,axis=1)
           )
    print (df)
    

    【讨论】:

      【解决方案2】:

      尝试使用这个长:

      df1 = df.loc[df['status'] == "active"]
      df2 = df.loc[df['status'] == "finished"]
      df1 = df1.groupby("ID")['value'].last()
      df2 = df2.groupby("ID")['value'].last()
      IDs = df["ID"].drop_duplicates()
      new_df = pd.DataFrame({"ID": IDs, "New1": df1.reindex(IDs).tolist(), "New2": df2.reindex(IDs).tolist()})
      print(new_df)
      

      输出:

        ID  New1   New2
      0  1   300    NaN
      1  2   500  400.0
      

      【讨论】:

        猜你喜欢
        • 2021-04-02
        • 2015-07-19
        • 1970-01-01
        • 1970-01-01
        • 2019-05-19
        • 1970-01-01
        • 2013-01-15
        • 2021-04-23
        • 1970-01-01
        相关资源
        最近更新 更多