gpt4 book ai didi

python - 查找日期列表中的日期差异 [Pandas]

转载 作者:行者123 更新时间:2023-12-01 04:09:11 30 4
gpt4 key购买 nike

假设以下数据集已排序日期列表:

dates=pd.DataFrame(data={'client':['1','2'],
'date':[['2012-3-10','2012-3-11','2012-3-12','2012-3-13','2012-3-14'],
['2012-3-12','2012-3-13','2012-3-16','2012-3-23']]})

sample dataset

我想找出平均日期差异(以天为单位)因此,例如,对于 Client '2'平均时滞 将为 2.75

最佳答案

开头为:

  client                                               date
0 1 [2012-3-10, 2012-3-11, 2012-3-12, 2012-3-13, 2...
1 2 [2012-3-12, 2012-3-13, 2012-3-16, 2012-3-23]

你可以

dates.groupby('client')['date'].apply(lambda x: [i / np.timedelta64(1, 'D') for i in np.diff([pd.to_datetime(c) for c in x])[0]])

获取timedelta(以为单位):

client
1 [1.0, 1.0, 1.0, 1.0]
2 [1.0, 3.0, 7.0]

dates.groupby('client')['date'].apply(lambda x: np.mean([i / np.timedelta64(1, 'D') for i in np.diff([pd.to_datetime(c) for c in x])[0]]))

对于平均值:

client
1 1.000000
2 3.666667

关于python - 查找日期列表中的日期差异 [Pandas],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35167056/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com