查找 pandas 平均时间列 [英] Finding the average time of pandas column

查看:36
本文介绍了查找 pandas 平均时间列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的熊猫格式如下:

title   |   decision   |   Time submitted
Book1   |      1       |   1486507594
Book1   |      2       |   1485450353

我想做的是找到决策为1的书的平均提交时间,然后找到决策为2的书的平均提交时间.我尝试使用:

What I would like to do is find the average time of submission for books with decision = 1 and then average submission time for books with decision = 2. I have tried using:

df_avg.loc[df_avg['decision'] == 2, 'submitted'].sum()
df_avg.loc[df_avg['decision'] == 1, 'submitted'].sum()

,但有时无法正常工作.我什至尝试在使用datetime将时间转换为日期和时间之前和之后进行上述操作.任何有关如何执行此操作的想法将不胜感激.

but it does not work for times. I even tried doing the above before and after converting the times to the date and time using datetime. Any ideas on how to do this would be greatly appreciated.

推荐答案

我认为您可以先将日期时间转换为 ns Unix格式,然后再转换为

I think you can convert datetime to ns unix format first and then groupby with aggregate mean:

print (df_avg)
   title  decision  Time submitted
0  Book1         1      1486507594
1  Book1         1      1486500012
2  Book1         2      1485480353
3  Book1         2      1485450353

df_avg['Time submitted'] = pd.to_datetime(df_avg['Time submitted'], unit='s')
                             .values.astype(np.int64)

df = df_avg.groupby('decision', as_index=False)['Time submitted'].mean()
df['Time submitted'] = pd.to_datetime(df['Time submitted'], unit='ns')
print (df)
   decision      Time submitted
0         1 2017-02-07 21:43:23
1         2 2017-01-26 21:15:53

但是对您来说,数据还可以在 10 ** 9 上的多个 second unix数据中起作用:

But for you data also works multiple seconds unix data to 10**9:

df = (df_avg['Time submitted'] * 10**9).groupby(df_avg['decision']).mean().reset_index()
df['Time submitted'] = pd.to_datetime(df['Time submitted'], unit='ns')
print (df)
   decision      Time submitted
0         1 2017-02-07 21:43:23
1         2 2017-01-26 21:15:53

这篇关于查找 pandas 平均时间列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆