擅长:python、mysql、java
<p>问题是<code>pandas</code>需要<code>datetime</code>s或<code>timedelta</code>s作为<code>diff</code>函数,因此首先通过<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.to_timedelta.html" rel="nofollow noreferrer">^{<cd5>}</a>进行转换,然后得到<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.dt.total_seconds.html" rel="nofollow noreferrer">^{<cd6>}</a>并除以<code>60</code>:</p>
<pre><code>df['Time_diff'] = pd.to_timedelta(df['Time'].astype(str)).diff(-1).dt.total_seconds().div(60)
#alternative
#df['Time_diff'] = pd.to_datetime(df['Time'].astype(str)).diff(-1).dt.total_seconds().div(60)
print (df)
Dev_id Time Time_diff
0 88345 13:40:31 19.966667
1 87556 13:20:33 15.550000
2 88955 13:05:00 49.533333
3 85678 12:15:28 NaN
</code></pre>
<p>如果想要<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.dt.floor.html" rel="nofollow noreferrer">^{<cd8>}</a>或{a4}每分钟:</p>
^{pr2}$