如何获得 Pandas 每天的平均送货时间(存储为 timedelta64[ns])?
How can i get the average delivery time (stored as timedelta64[ns]) per day on Pandas?
所以,我在 pandas 上有以下数据框:
我需要创建另一个包含每天平均送货时间的数据框,因为我很难完成这项任务。
因此,如您所见,我拥有 2018 年的所有 365 天,我需要计算用户在收到订单之前必须等待的平均时间。我只需要小时,分钟和秒,因为天数永远不会大于0。而且交货时间是timedelta64格式。
谢谢大家!
用户分组和均值
import pandas as pd
from io import StringIO
#Data preprocessing(ignore)
data = StringIO('''
2018-01-01 ,0 days 00:58:26
2018-01-01 ,0 days 01:27:04
2018-01-01 ,0 days 00:17:27
2018-01-01 ,0 days 00:14:26
2018-01-01 ,0 days 01:08:33
''')
#Converting to datetime and timedelta object
df = pd.read_csv(data,names=['date','delivery_time'],parse_dates=['date'])
df['delivery_time'] = pd.to_timedelta(df['delivery_time'])
#Grouping by date and then finding mean of delivery time
df.groupby(['date']).mean(numeric_only=False)
输出:
delivery_time
date
2018-01-01 0 days 00:49:11.200000
使用 dt.components
您可以轻松地从 timedelta
列中提取小时、分钟和秒。像下面这样的东西应该会产生你的结果。
df.groupby(['date']).mean(numeric_only=False)['delivery_time'].dt.components.iloc[:, 1:4]
这将为您提供新数据帧中每天的平均送货时间(timedelta[ns]
):
- 使用
numpy
的 timedelta64[ns]
将总交付时间转换为总秒数
import pandas as pd
import numpy as np
df['del_time_sec'] = df[:8]['delivery_time'] / np.timedelta64(1, 's')
- 获取平均时间,使用
GroupBy.transform
,每天使用 pandas.Series.dt.day
df['avg_sec'] = (df.groupby(df['date'].dt.day)['del_time_sec'].transform('mean'))
- 转换回日期时间,并在新的数据框中得到你的答案:
df['AVG_del_time_perday'] = pd.to_timedelta(df['avg_sec'], unit='s')
res = df[['date','AVG_del_time_perday']].drop_duplicates()
res
Out[116]:
date AVG_del_time_perday
0 2018-01-01 0 days 00:49:11.200000
5 2018-12-31 0 days 00:24:16.666666667
所以,我在 pandas 上有以下数据框:
我需要创建另一个包含每天平均送货时间的数据框,因为我很难完成这项任务。
因此,如您所见,我拥有 2018 年的所有 365 天,我需要计算用户在收到订单之前必须等待的平均时间。我只需要小时,分钟和秒,因为天数永远不会大于0。而且交货时间是timedelta64格式。
谢谢大家!
用户分组和均值
import pandas as pd
from io import StringIO
#Data preprocessing(ignore)
data = StringIO('''
2018-01-01 ,0 days 00:58:26
2018-01-01 ,0 days 01:27:04
2018-01-01 ,0 days 00:17:27
2018-01-01 ,0 days 00:14:26
2018-01-01 ,0 days 01:08:33
''')
#Converting to datetime and timedelta object
df = pd.read_csv(data,names=['date','delivery_time'],parse_dates=['date'])
df['delivery_time'] = pd.to_timedelta(df['delivery_time'])
#Grouping by date and then finding mean of delivery time
df.groupby(['date']).mean(numeric_only=False)
输出:
delivery_time
date
2018-01-01 0 days 00:49:11.200000
使用 dt.components
您可以轻松地从 timedelta
列中提取小时、分钟和秒。像下面这样的东西应该会产生你的结果。
df.groupby(['date']).mean(numeric_only=False)['delivery_time'].dt.components.iloc[:, 1:4]
这将为您提供新数据帧中每天的平均送货时间(timedelta[ns]
):
- 使用
numpy
的timedelta64[ns]
将总交付时间转换为总秒数
import pandas as pd
import numpy as np
df['del_time_sec'] = df[:8]['delivery_time'] / np.timedelta64(1, 's')
- 获取平均时间,使用
GroupBy.transform
,每天使用pandas.Series.dt.day
df['avg_sec'] = (df.groupby(df['date'].dt.day)['del_time_sec'].transform('mean'))
- 转换回日期时间,并在新的数据框中得到你的答案:
df['AVG_del_time_perday'] = pd.to_timedelta(df['avg_sec'], unit='s')
res = df[['date','AVG_del_time_perday']].drop_duplicates()
res
Out[116]:
date AVG_del_time_perday
0 2018-01-01 0 days 00:49:11.200000
5 2018-12-31 0 days 00:24:16.666666667