简体   繁体   English

行之间的时间差熊猫数据框

[英]Time difference between rows pandas dataframe

I have a time variable in my dataframe and I am trying to come up with a new column that gives the time passed in minutes from one row to another.我的数据框中有一个时间变量,我正在尝试提出一个新列,该列给出从一行到另一行传递的时间(以分钟为单位)。 I was trying to use this function:我试图使用这个功能:

df["Time"].diff()

But I get the following error:但我收到以下错误:

 TypeError: unsupported operand type(s) for -: 'datetime.time' and 'datetime.time'

Is there any way I can create a column in pandas with the time difference from one row to the next one?有什么方法可以在 pandas 中创建一列,从一行到下一行的时间差?

您可以将Time列转换为具有默认相同datedatetimes时间,因此Series.diff运行良好:

df['difference'] = pd.to_datetime(df["Time"].astype(str)).diff()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM