我有DataFrame与客户的协议如下:
rng = pd.date_range('2020-12-01', periods=5, freq='D')
df = pd.DataFrame({ "ID" : ["1", "2", "1", "2", "2"], "Date": rng})我需要创建基于以上df计算的新DataFrame:
为了更精确,我需要创建如下所示的df:

发布于 2020-12-16 08:19:25
使用Series.rsub从右侧减去今天,并通过Series.dt.days将时间增量转换为天,然后根据GroupBy.agg对每个组的GroupBy.first和GroupBy.last值进行聚合:
now = pd.to_datetime('today')
df = (df.assign(new = df['Date'].rsub(now).dt.days)
.groupby('ID').agg(New1 = ('new', 'first'),
New2 = ('new', 'last')))
.reset_index()
print (df)
ID New1 New2
0 1 15 13
1 2 14 11发布于 2020-12-16 08:20:46
也许可以试试groupby
New1 = pd.to_datetime('today') - df.groupby("ID")['Date'].min()
New2 = pd.to_datetime('today') - df.groupby("ID")['Date'].max()
df2 = pd.DataFrame({'ID': df['ID'].drop_duplicates(), 'New1': New1.tolist(), 'New2': New2.tolist()})
print(df2)输出:
ID New1 New2
0 1 15 days 13 days
1 2 14 days 11 dayshttps://stackoverflow.com/questions/65319496
复制相似问题