[英]Pandas - New column is a difference between 2 timestamps. How to convert this into hours?
Original data 原始数据
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
After this, I read the csv using read_csv and created a new column diff that's 之后,我使用read_csv读取了csv,并创建了一个新的列diff
df['diff'] = pd.to_datetime(df['Install Time']) - pd.to_datetime(df['Touch Time'])
This creates a column diff: 这将创建列差异:
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
For my analysis, I want to convert the values in diff column into hours and then plot it using matplotlib. 为了进行分析,我想将diff列中的值转换为小时,然后使用matplotlib对其进行绘制。
I want the final data to look like: 我希望最终数据如下所示:
0 days 01:28:00 should reflect as 1
1 days 02:58:00 should reflect as 26
使用total_seconds
然后除以3600可转换为小时。
df['diff_hours'] = df['diff'].dt.total_seconds() / 3600
另一种方法是:
df['diff_hours']=df['diff']/np.timedelta64(1,'h')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.