[Solved] Delete Column IF all column cell contain NULL value [closed]


Ok, as now we have seen your attempt, I would suggest you to grasp some concepts of Python and get an understanding of Python’s libraries and builtins, esp, in this case, you need to understand zip and all. You also need to understand about iterable and List Comprehension.

Zip is a wonderful function, and comes handy with the splat operator * when transposing Matrix. In this case, as you can access the rows through index but not the columns (unlike in Numpy), your first attempt should be to transpose the data.

>>> Matrix = [[['23'],['47'],['35'],['-']],
          [['45'],['22'],['34'],['-']],
          [['11'],['43'],['22'],['-']]]
>>> zip(*Matrix)
[(['23'], ['45'], ['11']), (['47'], ['22'], ['43']), (['35'], ['34'], ['22']), (['-'], ['-'], ['-'])]

Once you have all the Column data in a single row, your next attempt should be to find out, which row contains all [‘-‘]. You can use the builtin all which returns true if all the elements are true.

You also need to know, how to navigate through the rows in the transpose Matrix. List Comprehension and or generator expression comes handy

[row for row in zip(*Matrix) if all(e != ['-'] for e in row)]

which is an equivalent concise notation for the following loop

_Matrix = []
for row in  zip(*Matrix):
    #all(e != ['-'] for e in row)
    _cond = True
    for e in row:
        if e != ['-']:
               _cond = False
               break
    if not _cond:
        _Matrix.append(e)

Now, once you remove the target row, you need to transpose back the data to get the original form, sans the column with all the [‘-‘] removed

zip(*(row for row in zip(*Matrix) if all(e != ['-'] for e in row)))

1

solved Delete Column IF all column cell contain NULL value [closed]