如果满足条件,则从记录数组中删除行

时间:2018-12-03 04:19:14

标签: python numpy

如果要满足条件,我想从numpy recarray中删除行:

这是我尝试过的:

<html>
<head>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
</head>
<body>

<form action="http://google.ca" id='idForm'>
      <div>
        <button type='submit' >post</button>
      </div>
     </form>
  </body>  
<script>

    $("#idForm").submit(function(e) {

        var form = $(this);
        var url = form.attr('action');

        $.ajax({
               type: "POST",
               url: url,
               data: form.serialize(), // serializes the form's elements.
               success: function(data)
               {
                   alert(data); // show response from the php script.
               }
             });

        e.preventDefault(); // avoid to execute the actual submit of the form.
    });

</script>
</html>

但是,我收到了:

for i in range(0, len(data)):
    if 270 <= data['l'][int(i)] <= 350 and -20 <= data['b'][int(i)] <= 20:
        np.delete(data, data[int(i)])

如果满足多个条件,是否有(理想的快速)方式从recarray中删除行?

2 个答案:

答案 0 :(得分:0)

一个简单问题的简单答案就是:

我要的数字是<270 AND> 350,这当然是不可能的!使用or运算符!

 datam[(datam['l'] < 270) | (datam['l'] > 350) | (datam['b'] < -20) | (datam['b'] > 20)] 

几乎立即运行了超过800万个数据点。

答案 1 :(得分:0)

根据您的代码,该测试似乎可以正常运行,但是.delete看起来有问题。这是一个示例:

        import numpy as np
        import random

        np.set_printoptions(threshold=np.nan)
        dtype_arr = []
        x_range_upper_limit = 12
        y_range = "abcdefghijklm"
        for letter in y_range: # abcdefghijklmnopqrstuvwxyz
            dtype_arr.append((letter, 'int'))
        data = np.recarray((x_range_upper_limit,),dtype=dtype_arr)
        for letter in range(0,x_range_upper_limit):
            for i in y_range:
                data[letter][i]=random.randint(1,101)
        print ("data length:{}".format(len(data)))
        # ensure a row to delete
        data['l'][3] = 280
        data['b'][3] = 10
        # test code
        print (data)
        list_of_rows_to_delete = []
        for i in range(0, x_range_upper_limit):
            if 270 <= data['l'][i] <= 350 and -20 <= data['b'][i] <= 20:
                list_of_rows_to_delete.append(i)
        print ("list of rows to delete: {}".format(list_of_rows_to_delete))
        data=np.delete(data, list_of_rows_to_delete)
        print (len(data))
        print (data)

输出:

        data length:12
        [(74, 14,  53, 54, 99, 35, 31, 88, 49,  8, 50,   8, 30)
        (26, 11,   4, 47, 84, 88, 73, 55, 58, 61, 68, 101, 58)
        (64, 33,   7, 82, 32, 44,  1, 34, 47, 60, 38,  56, 17)
        (87, 10,  24, 71, 54, 49, 73, 42, 86,  1, 70, 280, 55)
        (87, 80, 100, 97,  9, 41, 99, 51, 97, 13, 46,  81, 34)
        (72, 18,  88, 14, 42, 98, 50, 10, 70, 35, 33,  31, 18)
        (59, 53,  98, 22, 50, 14, 29, 84, 27, 70, 90,   7, 58)
        (47, 26,  27, 48, 79, 37, 97, 64, 64, 59, 15,  52, 32)
        (62, 75,  85, 36, 38, 53, 33, 98, 54,  2, 33,   9, 21)
        (81, 97,  96, 58, 40, 50, 32,  4, 94,  9, 16,  79, 50)
        (75, 66,  90, 29, 94, 48, 44, 28, 41, 18, 45,  65, 50)
        (20, 60,  25, 69, 40, 90, 99, 79, 30,  8, 23,  27, 25)]
        list of rows to delete: [3]
        11
        [(74, 14,  53, 54, 99, 35, 31, 88, 49,  8, 50,   8, 30)
        (26, 11,   4, 47, 84, 88, 73, 55, 58, 61, 68, 101, 58)
        (64, 33,   7, 82, 32, 44,  1, 34, 47, 60, 38,  56, 17)
        (87, 80, 100, 97,  9, 41, 99, 51, 97, 13, 46,  81, 34)
        (72, 18,  88, 14, 42, 98, 50, 10, 70, 35, 33,  31, 18)
        (59, 53,  98, 22, 50, 14, 29, 84, 27, 70, 90,   7, 58)
        (47, 26,  27, 48, 79, 37, 97, 64, 64, 59, 15,  52, 32)
        (62, 75,  85, 36, 38, 53, 33, 98, 54,  2, 33,   9, 21)
        (81, 97,  96, 58, 40, 50, 32,  4, 94,  9, 16,  79, 50)
        (75, 66,  90, 29, 94, 48, 44, 28, 41, 18, 45,  65, 50)
        (20, 60,  25, 69, 40, 90, 99, 79, 30,  8, 23,  27, 25)]