Python - Iterrow通过pandas数据帧并分配和有条件地更新datetime变量

时间:2016-07-13 23:32:11

标签: python loops datetime if-statement pandas

我是Python新手,想知道是否有人可以帮助我。

我想在pandas数据框中迭代datetime列,而对于每次迭代,更新一个具有最近时间的变量。我们假设这是我的数据:

    Time
06:12:50
06:13:51
06:13:51
06:13:50
06:14:51
06:14:49

对于我的结果,我希望它看起来像这样:

RecentTime:
   06:12:50
   06:13:51
   06:13:51
   06:13:51
   06:14:51
   06:14:51

我认为代码应该看起来像这样,但我遇到了麻烦,无法弄清楚原因。这是我的代码:

RecentTime = [] # Store list of most recent time for each row
Index: None       # Create empty variable
# Loop through 
for index, row in data.iterrows():
    index = row['Time']   # Save value as index
    if index >= row['Time']: # If time is greater than current row
    index = row['Time']
        RecentTime.append(index) # Append most recent variable into list
    else:
        continue

出于某种原因,这是我的结果:

RecentTime
  06:12:50
  06:13:51
  06:13:51
  06:13:50
  06:14:51
  06:14:49

1 个答案:

答案 0 :(得分:1)

每次循环都是在检查不等式之前写入变量Error: Invalid XRef stream header pdf.worker.js (line 250) <System> XRef_readXRef@resource://pdf.js/build/pdf.worker.js:3693:13 XRef_parse@resource://pdf.js/build/pdf.worker.js:3289:23 PDFDocument_setup@resource://pdf.js/build/pdf.worker.js:2484:7 PDFDocument_parse@resource://pdf.js/build/pdf.worker.js:2371:7 LocalPdfManager_ensure/<@resource://pdf.js/build/pdf.worker.js:1934:20 LocalPdfManager_ensure@resource://pdf.js/build/pdf.worker.js:1929:1 BasePdfManager_ensureDoc@resource://pdf.js/build/pdf.worker.js:1861:14 loadDocument/</<@resource://pdf.js/build/pdf.worker.js:33208:11 pdf.worker.js (line 252) <System> Warning: Unsupported feature "unknown" pdf.worker.js (line 235) <System> Warning: Unsupported feature "unknown" Warning: Indexing all PDF objects pdf.worker.js (line 235) <System> PDF 0d3cdf6286f2947410f0d270a249f816 [1.4 iText 2.1.3 (by lowagie.com) kSar Version:5.0.6] (PDF.js: 1.0.1149) Error: Bad uncompressed block length in flate stream pdf.worker.js (line 250) <System> FlateStream_readBlock@resource://pdf.js/build/pdf.worker.js:31244:9 DecodeStream_getByte@resource://pdf.js/build/pdf.worker.js:30882:9 Lexer_nextChar@resource://pdf.js/build/pdf.worker.js:30125:34 Lexer@resource://pdf.js/build/pdf.worker.js:30067:5 EvaluatorPreprocessor@resource://pdf.js/build/pdf.worker.js:12105:30 PartialEvaluator_getOperatorList@resource://pdf.js/build /pdf.worker.js:10616:26 Page_getOperatorList/pageListPromise<@resource://pdf.js/build/pdf.worker.js:2216:16 pdf.worker.js (line 252) <System> Warning: Unsupported feature "unknown" pdf.worker.js (line 235) ,所以

index

不仅始终是if index >= row['Time']: ,而且在检查此不等式之前,始终将索引设置为等于当前时间。根据您的描述中的模式,在期望的结果中,时间永远不会早于上一行,我认为您正在寻找更像这样的内容:

True

最后,行RecentTime = [] # Store list of most recent time for each row priortime = None # Loop through for index, row in data.iterrows(): currenttime = row['Time'] if priortime is None: priortime = currenttime if priortime > currenttime: # If prior time is greater than current row currenttime = priortime priortime = currenttime RecentTime.append(currenttime) 应该抛出错误Index: None。假设您要为变量赋值,请使用SyntaxError: invalid syntaxIndex = None,小写,已在数据框循环中用于引用数据框中的索引值,因此即使您的大写index变量不会发生冲突,您也应该将其命名为其他内容。