[英]Calculate time difference between last row and all other rows in a dataframe in Python
I have a bunch of dataframes that all look like this我有一堆看起来像这样的数据框
DATE A B
2021-01-01 1 2
2021-01-05 1 2
2021-01-06 1 2
2021-01-10 1 2
2021-01-20 1 2
I would like to calculate the diffence in time between all rows and the last row.我想计算所有行和最后一行之间的时间差异。 Meaning I'd like to create another column that contains the time difference between that row and the last row in the dataframe.这意味着我想创建另一列,其中包含该行与 dataframe 中最后一行之间的时间差。 So it should look like this:所以它应该是这样的:
DATE A B timediff
2021-01-01 1 2 19 days
2021-01-05 1 2 14 days
2021-01-06 1 2 13 days
2021-01-10 1 2 9 days
2021-01-20 1 2 0 days
Is there a way to do this?有没有办法做到这一点? Date is already a datetime variable. Date 已经是一个日期时间变量。
Thanks谢谢
You can subtract the last row and take absolute:您可以减去最后一行并取绝对值:
df['timediff_days'] =df['DATE'].sub(df['DATE'].iloc[-1]).dt.days.abs()
Or:或者:
df['timediff'] = pd.Timedelta(0,unit='d')-df['DATE'].sub(df['DATE'].iloc[-1])
DATE A B timediff
0 2021-01-01 1 2 19 days
1 2021-01-05 1 2 15 days
2 2021-01-06 1 2 14 days
3 2021-01-10 1 2 10 days
4 2021-01-20 1 2 0 days
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.