【问题标题】:How to extract rows from numpy array where value in col1 = a如何从numpy数组中提取行,其中col1 = a
【发布时间】:2020-09-21 09:53:17
【问题描述】:

我每年有一个二维温度值数组,其中每年都有几个值。所以数组看起来像这样:

[[1960, a, b, c, d, e, f ...],
 [1960, a1, b1, c1, d1, e1, f1 ...],
 [1960, a2, b2, c2, d2, e2, f2 ...],
 [1961, a, b, c, d, e, f ...],
 [1961, a1, b1, c1, d1, e1, f1 ...],
 [1961, a2, b2, c2, d2, e2, f2 ...],
 [1962, a, b, c, d, e, f ...],
 [1962, a1, b1, c1, d1, e1, f1 ...],
 [1962, a2, b2, c2, d2, e2, f2 ...]....]

我需要为第 0 列是 1961 的行提取第 1 列中的所有内容(因此所有 a 值)。

有人知道我会怎么做吗? 谢谢!

【问题讨论】:

    标签: python numpy numpy-ndarray data-processing numpy-slicing


    【解决方案1】:

    让 L 成为你的数组。然后使用下面的代码。

    L[L[:, 1] == 1960][:, 0] 
    

    【讨论】:

      【解决方案2】:

      适用于常规 python 列表的解决方案:

      [item[1] for item in array if item[0]==1961]
      

      array 是您的二维输入数组。

      【讨论】:

        【解决方案3】:

        使用切片
        选择第二个值

        a[a[:, 0] == '1961', 1]
        

        或所有值

        a[a[:, 0] == '1961']
        

        【讨论】:

          猜你喜欢
          • 2011-12-14
          • 2023-02-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-03-15
          • 1970-01-01
          相关资源
          最近更新 更多