1 回答
TA贡献1796条经验 获得超7个赞
您可以MultiIndex.from_product使用pd.date_range:
dates = pd.date_range(df["sale_month"].min(), df["sale_month"].max(), freq="MS")
s = pd.MultiIndex.from_product([df["ID"].unique(), reversed(dates)],names=df.columns[:2])
df = df.set_index(["ID","sale_month"]).reindex(s).reset_index()
df["attribute1"] = df.groupby('ID')["attribute1"].transform("first")
print (df)
ID sale_month attribute1 attribute2
0 1 2020-06-01 blue 1.0
1 1 2020-05-01 blue 2.0
2 1 2020-04-01 blue 3.0
3 1 2020-03-01 blue 4.0
4 2 2020-06-01 yellow NaN
5 2 2020-05-01 yellow 5.0
6 2 2020-04-01 yellow 4.0
7 2 2020-03-01 yellow 3.0
8 3 2020-06-01 green NaN
9 3 2020-05-01 green 7.0
10 3 2020-04-01 green 8.0
11 3 2020-03-01 green NaN
添加回答
举报