【发布时间】:2021-01-23 07:06:13
【问题描述】:
我有一个数据框 dataframe,由两列 customer_id 和一个日期列 created_at 组成。
我希望为客户保留在客户群中的每个月添加另一行。
例如,如果 customer_id 是在 7 月创建的,则数据框会在“created_at”和“today”的范围内为该客户添加 4 行额外的行。例如;对于 customer1,我将有 9 行,每个月最多一天,对于 customer2:7 行,而 customer3:4 行。我在想可能像我在下面复制的东西,将 df 与 seqDates 合并的想法......
import pandas as pd
import numpy as np
df = pd.DataFrame([("customer1", "05-02-2020"), ("customer2","05-04-2020"), ("customer3","04-07-2020")], index=["1","2","3"], columns= ("customer_id","created_at"))
df["created_at"] = pd.to_datetime(df["created_at"])
# create month expansion column
start = min(df["created_at"])
end = pd.to_datetime("today")
seqDates = pd.date_range(start, end, freq="D")
seqDates = pd.DataFrame(seqDates)
columns = ["created_at"]
【问题讨论】:
标签: python pandas numpy date expand