[Python] Remove rows in a 2d-numpy array if they contain a specific element

Discussão em 'Python' iniciado por Stack, Julho 11, 2025.

  1. Stack

    Stack Membro Participativo

    I have a matrix as 2d-np.array and I would like to remove all rows that contain an element x in a specific column. My goal is to return a matrix without these rows, so it should be smaller. My function looks like this:

    def delete_rows(matrix, x, col):
    for i in range(matrix.shape[0]-1):
    if(matrix[i,col] == x):
    np.delete(matrix, i, axis = 0)
    return matrix


    Sadly in my test the shape of the matrix stayed the same after removing rows. I think the deleted rows were substituted by rows with 0s. Any advice on how I can achieve my goal?

    Continue reading...

Compartilhe esta Página