Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove row or column from 2D list if all values (in that row or column) are None

Tags:

python

list

I have a grid (6 rows, 5 columns):

grid = [
        [None, None, None, None, None],
        [None, None, None, None, None],
        [None, None, None, None, None],
        [None, None, None, None, None],
        [None, None, None, None, None],
        [None, None, None, None, None],
        ]

I augment the grid and it might turn into something like:

grid = [
        [{"some" : "thing"}, None, None, None, None],
        [None, None, None, None, None],
        [None, None, None, None, None],
        [None, None, None, {"something" : "else"}, None],
        [None, {"another" : "thing"}, None, None, None],
        [None, None, None, None, None],
        ]

I want to remove entire rows and columns that have all Nones in them. So in the previous code, grid would be transformed into:

grid = [
        [{"some" : "thing"}, None, None],
        [None, None, {"something" : "else"}],
        [None, {"another" : "thing"}, None],
        ]

I removed row 1, 2, 5 (zero indexed) and column 2 and 4.

The way I am deleting the rows now:

for row in range(6):
    if grid[row] == [None, None, None, None, None]:
        del grid[row] 

I don't have a decent way of deleting None columns yet. Is there a "pythonic" way of doing this?

like image 940
Matt Avatar asked Dec 31 '09 04:12

Matt


People also ask

How do I remove a specific element from a list in Python 2d?

Python pop() method The pop() method removes an element from the list based on the index given.

How do I drop a row based on a column value?

Use drop() method to delete rows based on column value in pandas DataFrame, as part of the data cleansing, you would be required to drop rows from the DataFrame when a column value matches with a static value or on another column value.

How do you delete a row from a certain value?

Go ahead to right click selected cells and select the Delete from the right-clicking menu. And then check the Entire row option in the popping up Delete dialog box, and click the OK button. Now you will see all the cells containing the certain value are removed.

How do you delete a row from an array in Python?

Using the NumPy function np. delete() , you can delete any row and column from the NumPy array ndarray . Specify the axis (dimension) and position (row number, column number, etc.). It is also possible to select multiple rows and columns using a slice or a list.


1 Answers

It's not the fastest way but I think it's quite easy to understand:

def transpose(grid):
    return zip(*grid)

def removeBlankRows(grid):
    return [list(row) for row in grid if any(row)]

print removeBlankRows(transpose(removeBlankRows(transpose(grid))))

Output:

[[{'some': 'thing'}, None, None],
 [None, None, {'something': 'else'}],
 [None, {'another': 'thing'}, None]]

How it works: I use zip to write a function that transposes the rows and columns. A second function removeBlankRows removes rows where all elements are None (or anything that evaluates to false in a boolean context). Then to perform the entire operation I transpose the grid, remove blank rows (which are the columns in the original data), transpose again, then remove blank rows.

If it's important to only strip None and not other things that evaluate to false, change the removeBlankRows function to:

def removeBlankRows(grid):
    return [list(row) for row in grid if any(x is not None for x in row)]
like image 125
Mark Byers Avatar answered Oct 20 '22 00:10

Mark Byers