使用 Groupby 的 Python 最小/最大日期

时间:2021-05-21 16:57:17

标签: python pandas dataframe

我正在尝试获取特定价格的开始日期和结束日期。在我的示例中,几天的价格是 3 美元,然后几天增加到 4 美元,然后又回到 3 美元。

import pandas as pd

df = pd.DataFrame([
    {"Price":3,"ds":"2017-01-01"},
    {"Price":3,"ds":"2017-01-02"},
    {"Price":3,"ds":"2017-01-03"},
    {"Price":3,"ds":"2017-01-04"},
    {"Price":3,"ds":"2017-01-05"},
    {"Price":4,"ds":"2017-01-06"},
    {"Price":4,"ds":"2017-01-07"},
    {"Price":4,"ds":"2017-01-08"},
    {"Price":4,"ds":"2017-01-09"},
    {"Price":3,"ds":"2017-01-10"},
    {"Price":3,"ds":"2017-01-11"},
    {"Price":3,"ds":"2017-01-12"},
    {"Price":3,"ds":"2017-01-13"},
    {"Price":3,"ds":"2017-01-14"}
    ])

start = df.groupby(["Price"])["ds"].min().reset_index()
end = df.groupby(["Price"])["ds"].max().reset_index()
df2 = start.merge(end, how = 'left', on = ["Price"])
df2.rename(columns = {"ds_x":"start_dt", "ds_y":"end_dt"},inplace=True)

输出:

   Price start_dt    end_dt
     3  2017-01-01  2017-01-14
     4  2017-01-06  2017-01-09

期望的输出:

   Price start_dt    end_dt
     3  2017-01-01  2017-01-05
     4  2017-01-06  2017-01-09
     3  2017-01-10  2017-01-14

0 个答案:

没有答案