Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter out columns that are all 0s in Python?

Tags:

python

I have some structures that needs to be filtered. Is there a way to do this nicely in Python?

I have an ugly way of doing this, but I want to clean it up:

original_header = ['a','b','c']
original_rows = [[1,0,1], [0,0,0], [1,0,0]]

processed_header, processed_rows = some_cool_utility(original_header, original_rows)

assert_equals(['a', 'c'], processed_header)
assert_equals([[1,1], [0,0], [1,0]], processed_rows)
like image 277
pjotr_dolphin Avatar asked Aug 17 '12 12:08

pjotr_dolphin


2 Answers

original_header = ['a','b','c']
original_rows = [[1,0,1], [0,0,0], [1,0,0]]

#transpose rows to get columns
columns = zip(*original_rows)

#build list which is true if the column should be kept (is not a column of all zeros)  
not_all_zero = [  any(x) for x in columns ]

#filter the lists based on columns
processed_header = [x for i,x in enumerate(original_header) if not_all_zero[i] ]
processed_columns = [ x for i,x in enumerate(columns) if not_all_zero[i] ]

#transpose the remaining columns back into rows.
processed_rows = zip(*processed_columns)

print (processed_header)  #['a', 'c']
print (processed_rows)    #[(1, 1), (0, 0), (1, 0)]

Note that this returns a list of tuples instead of a list of lists. If you really need a list of lists, you can just processed_rows = map(list, processed_rows)

like image 169
mgilson Avatar answered Nov 10 '22 23:11

mgilson


Use NumPy

import numpy as np

original_rows = np.asarray([[1,0,1], [0,0,0], [1,0,0]])
original_labels = np.asarray(["a", "b", "c"])

# Get locations where columns are all zeros.
nonzero_cols = np.any(original_rows!=0, axis=0)

# Get data only where column is not all zeros.
nonzero_data = original_rows[:, nonzero_cols]
nonzero_labels = original_labels[nonzero_cols]
like image 35
ely Avatar answered Nov 10 '22 21:11

ely