[英]Days calculation in DataFrame in Python Pandas?
I have DataFrame with clients' agreements like below:我有 DataFrame 与客户的协议如下:
rng = pd.date_range('2020-12-01', periods=5, freq='D')
df = pd.DataFrame({ "ID" : ["1", "2", "1", "2", "2"], "Date": rng})
And I need to create new DataFrame with calculation based on above df:我需要根据上面的df计算创建新的DataFrame:
To be more precision I need to create df like below:为了更精确,我需要创建 df 如下所示:
Use Series.rsub
for subtract from right side with today and convert timedeltas to days by Series.dt.days
and then aggregate by GroupBy.agg
for GroupBy.first
and GroupBy.last
values per groups:使用
Series.rsub
从右侧减去今天,并通过 Series.dt.days 将 timedeltas 转换为天数,然后通过Series.dt.days
GroupBy.first
GroupBy.agg
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
Maybe try groupby
:也许尝试
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)
Output: Output:
ID New1 New2
0 1 15 days 13 days
1 2 14 days 11 days
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.