【问题标题】:How to How to take one element from list, compare it with another from the same series?如何如何从列表中取出一个元素,将其与同一系列中的另一个元素进行比较?
【发布时间】:2021-02-06 18:42:32
【问题描述】:

我只需要从系列(“荧光”)中打印高于某个数值阈值的元素。

如何获取一个元素,将其与同一系列中的另一个元素进行比较,如果条件不满足,则直接在该系列中继续前进?

#for i in range(len(fluorescence)):
for i in fluorescence.iteritems():

   if i > 43: # (mean(fluorescence)-min(fluorescence)) == 43.
       print(i)

TypeError: 'tuple' 和 'float' 的实例之间不支持'

这是原始数据框:

我想要的是:

提前谢谢你

【问题讨论】:

  • 请使用完整的错误回溯更新您的问题。
  • 请提供预期的MRE - Minimal, Reproducible Example。显示中间结果与预期结果的偏差。我们应该能够将您的代码块粘贴到文件中,运行它并重现您的问题。这也让我们可以在您的上下文中测试任何建议。为符合本网站的宗旨,不接受站外链接和文本图像。
  • Include your minimal data frame 作为示例的一部分。您发布的代码无法运行。
  • 请不要发布图片,而是发布文本数据。

标签: python pandas loops series


【解决方案1】:

iteritems 返回一个 (key, value) 的元组。您不能将整个元组与标量进行比较。

if i[1] > 43:

应该可以解决您的问题。请参阅其他答案以获得更好的结果。

【讨论】:

    【解决方案2】:

    filter() 是你需要的。

    # Filter will filter an iterator if the function returns True
    fluorescence = [94, 35, 12, 104]
    result = list(filter(lambda x: x > 43, fluorescence))
    print(result)
    # [94, 104]
    

    【讨论】:

      【解决方案3】:

      Pandas tolist() 会将系列转换为易于迭代的列表。

      #an empty list to store the values
      new=[]
      for i in fluorescence.tolist():
      if i > 43
      new.append(i)
      print(new)
      #print statement not in the loop
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-03-21
        • 1970-01-01
        • 2020-03-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-30
        相关资源
        最近更新 更多