我有一个按 AccountID
和 PurchaseDate
排序的 df
。我想要做的是计算并创建每组 AccountID
中的 PurchaseDate
之间的差异的新列。
AccountID PurchaseDate Price
| 113 2018-09-01 22:56:30 13|
| 113 2018-09-02 22:56:30 19|
| 114 2018-09-01 22:56:30 20|
| 114 2018-09-03 22:56:30 25|
至
AccountID PurchaseDate Price DateDiff
| 113 2018-09-01 22:56:30 13 null|
| 113 2018-09-02 22:56:30 19 1 |
| 114 2018-09-01 22:56:30 20 null|
| 114 2018-09-03 22:56:30 25 2 |
最佳答案
你可以这样做:
df['DateDiff'] = df.groupby('AccountID')['PurchaseDate'].\
diff().apply(lambda x: x.days)
关于python - 如何计算Python中一组内的日期时间之间的差异?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54151378/