Python的大熊猫日期时间差

问题描述:

我有这样的:Python的大熊猫日期时间差

x[l[0]] = pd.to_datetime(x[l[0]], format="%Y-%m-%d %H:%M:%S") 

l=list(x)

我怎么能在几秒钟内这之间的区别对象,如果我这样做

x[l[0]][1]-x[l[0]][2] 

它返回我timedelta对象

print (x[:5]) 
       LogDate Query_BoxID_ID Query_Function_ID SC_Win32_Status 
0 2017-06-15 09:50:14    12     24    0 
1 2017-06-15 09:50:14    12     26    0 
2 2017-06-15 09:50:14    12     26    0 
3 2017-06-15 09:50:14    12     30    0 
4 2017-06-15 09:50:32    12     19    0 
+0

你可以添加一些样本数据,'打印(X [:5 ])? – jezrael

我可以做

(x[l[0]][1]-x[l[0]][2]).total_seconds() 

使用diff为timedeltas,这是由total_seconds转换:

#convert column to datetime 
x['LogDate'] = pd.to_datetime(x['LogDate'], format="%Y-%m-%d %H:%M:%S") 
#first value is NaN always, so replaced to 0 by fillna and cast to int 
a = x['LogDate'].diff().dt.total_seconds().fillna(0).astype(int) 
print (a) 
0  0 
1  0 
2  0 
3  0 
4 18 
Name: LogDate, dtype: int32 

b = int((x.loc[0, 'LogDate'] - x.loc[0, 'LogDate']).total_seconds()) 
print (b) 
0 
+0

但是我怎么能有前两行之间的区别?没有所有差异 –

+0

是的,total_seconds是解决方案。但是,大熊猫在所有专栏中都很重要,因此首先为所有差异添加了解决方案。 – jezrael