gpt4 book ai didi

python - 具有平均时间的 Pandas 数据透视表

转载 作者:太空宇宙 更新时间:2023-11-03 11:48:10 26 4
gpt4 key购买 nike

我一直在使用 pandas 对时间序列数据进行分析,但一直无法将它们集成到数据透视表中。我在 csv 中有一个数据:

gov start   end
a 2015-12-08T16:05:00.980+03 2015-12-08T16:14:31.765+03
a 2015-12-08T16:07:53.356+03 2015-12-08T16:34:43.413+03
b 2015-12-08T16:08:43.371+03 2015-12-08T16:54:32.257+03
b 2015-12-08T15:56:12.006+03 2015-12-08T17:35:04.499+03

我有一组简单的数据,包含 startend 时间,并从中算出两者之间的时间差:

piv_t_subset = pd.read_csv('time_test.csv', parse_dates=['start','end'])

piv_t_subset['time_diff'] = piv_t_subset['end'] - piv_t_subset['start']

我可以将时间的独立平均值计算为:

t = piv_t_subset['time_diff'].mean()
print t

0 days 00:18:53.703286

我想用这个时间信息创建一个数据透视表,但是当我尝试时:

pd.pivot_table(piv_t_subset,index=["gov"],values=['time_diff'],aggfunc=[np.mean])

我得到错误:

DataError: No numeric types to aggregate

我是否需要进行更多预处理才能将其从 timeseries 转换为 float

最佳答案

现在不支持link .

但您可以通过total_secondstimedelta64Series转换为floatSeries :

piv_t_subset['time_diff1'] = [td.total_seconds() for td in piv_t_subset['time_diff']]
print piv_t_subset
gov start end
0 a 2015-12-08 13:05:00.980 2015-12-08 13:14:31.765
1 a 2015-12-08 13:07:53.356 2015-12-08 13:34:43.413
2 b 2015-12-08 13:08:43.371 2015-12-08 13:54:32.257
3 b 2015-12-08 12:56:12.006 2015-12-08 14:35:04.499

piv_t_subset['time_diff'] = piv_t_subset['end'] - piv_t_subset['start']

piv_t_subset['time_diff1'] = [td.total_seconds() for td in piv_t_subset['time_diff']]
print piv_t_subset
gov start end time_diff \
0 a 2015-12-08 13:05:00.980 2015-12-08 13:14:31.765 00:09:30.785000
1 a 2015-12-08 13:07:53.356 2015-12-08 13:34:43.413 00:26:50.057000
2 b 2015-12-08 13:08:43.371 2015-12-08 13:54:32.257 00:45:48.886000
3 b 2015-12-08 12:56:12.006 2015-12-08 14:35:04.499 01:38:52.493000

time_diff1
0 570.785
1 1610.057
2 2748.886
3 5932.493

print piv_t_subset.groupby('gov').agg({'time_diff1':np.mean})
time_diff1
gov
a 1090.4210
b 4340.6895

#omit aggfunc, in pivot_table is default numpy.mean
print pd.pivot_table(piv_t_subset,index=["gov"],values=['time_diff1'])
time_diff1
gov
a 1090.4210
b 4340.6895

关于python - 具有平均时间的 Pandas 数据透视表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34268418/

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