熊猫-新栏是两个时间戳之间的差。如何将其转换为小时?

时间:2019-03-28 20:10:32

标签: python pandas datetime

原始数据

Touch Time        Install Time
3/28/2019 14:06  3/28/2019 15:34
3/27/2019 19:23  3/28/2019 15:22
3/28/2019 15:01  3/28/2019 15:18
3/28/2019 12:41  3/28/2019 15:18
3/27/2019 12:10  3/28/2019 15:08

此后,我使用read_csv读取了csv,并创建了一个新列diff,

df['diff'] =  pd.to_datetime(df['Install Time']) - pd.to_datetime(df['Touch Time'])

这将创建列差异:

      Touch Time     Install Time          diff
0   3/28/2019 14:06  3/28/2019 15:34    0 days 01:28:00
1   3/27/2019 19:23  3/28/2019 15:22    0 days 19:59:00
2   3/28/2019 15:01  3/28/2019 15:18    0 days 00:17:00
3   3/28/2019 12:41  3/28/2019 15:18    0 days 02:37:00
4   3/27/2019 12:10  3/28/2019 15:08    1 days 02:58:00

为进行分析,我想将diff列中的值转换为小时,然后使用matplotlib对其进行绘制。

我希望最终数据看起来像这样:

0 days 01:28:00 should reflect as 1 

1 days 02:58:00 should reflect as 26

2 个答案:

答案 0 :(得分:3)

使用total_seconds然后除以3600可转换为小时。

df['diff_hours'] = df['diff'].dt.total_seconds() / 3600

答案 1 :(得分:0)

另一种方法是:

df['diff_hours']=df['diff']/np.timedelta64(1,'h')