【问题标题】:How to I make a for/if loop that compares multiple rows and prints timestamps如何创建一个比较多行并打印时间戳的 for/if 循环
【发布时间】:2018-06-30 05:02:10
【问题描述】:

我目前正在尝试开发一个比较行内数据的代码。该程序的目标是比较速度差异,如果速度差异很大,我希望它打印时间戳。以下是我目前写的:

for row in cj1:
speed = row[1].speed
timestamp = row[0].timestamp
for i in range(1, row.length):
if speed is different =!:
      Calculate acceleration
if acceleration > #certain amount:
   print(timestamp)
Else:
   timestamp = rows[i].timestamp
   speed = rows[i].speed

这是一个非常粗略的代码,我不太清楚如何表达它。有人可以帮忙吗?

【问题讨论】:

  • 你在使用 Python 3 吗?
  • 是的,我正在使用 Python 3

标签: python dataframe timestamp data-science


【解决方案1】:

如果您只想计算代码需要多长时间。您可以使用此上下文管理器示例。

from time import time, sleep

class Time(object):
    def __enter__(self):
        self.start = time()
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.end = time()
        self.interval = self.end - self.start


with Time() as t:
    sleep(5)  # <- your code here

print(t.interval)

5.001040935516357

更新

所以你下次可以问一个更好的问题。以下是一些反馈:

for row in cj1:
    speed = row[1].speed                # <- There was no indent here.
    timestamp = row[0].timestamp
    for i in range(1, row.length):
        if speed is different =!:       # <- There was no indent here.
                                        # `=!` with out another value is a syntax error.
              Calculate acceleration    #  <- `Calculate` is not defined.
                                        #  Also no brackets are used,
                                        #  maybe `Calculate(acceleration)`?
        if acceleration > #certain amount:

                                        # `acceleration` is not defined.
                                        # You can not compare greater than with a comment.
           print(timestamp)
        Else:                           # < - `Else` cannot be capitalised. you mean. `else`
           timestamp = rows[i].timestamp
           speed = rows[i].speed

【讨论】:

  • 问题是关于比较数据对象的值,而不是代码速度本身。
  • 我已尽力提供帮助,但您的代码格式不正确,我不太明白您对 data object 的含义。
【解决方案2】:

也许你应该展示你的一段数据,它是什么样子的?
值的格式是什么?
您显示的代码有错误的缩进等。我想数据不能像 row[1].speed 那样读取,或者这真的有效吗?

编辑: 那是计算某种伪代码吗?请先尝试在 Python shell 中制定单行,看看它们是 Python 代码...

【讨论】:

  • 您现在应该知道,cmets 不是答案。请阅读the guidelines
  • 是的,评论会更好。很遗憾,我不被允许写它们。我想我可以修改我的答案,只要我能理解提问者想要做什么。
猜你喜欢
  • 1970-01-01
  • 2021-03-25
  • 1970-01-01
  • 2015-05-23
  • 2017-07-31
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
相关资源
最近更新 更多