Reputation: 57
I have an array and I want to delete some rows according to a criteria. The criteria is if a row has all elements as zero other than the first element, it should be deleted. So if tempn has only '0', that row is deleted. Consider this for narrays and ntemps (that is., no fixed rows or arrays)
my_input = [array([['temp1', '32F'],
['temp2', '243K'],
['temp3', '0'],
['temp4', '0'],
['temp5', '0'],
['temp6', '30C'],
['temp7', '0'],
['temp8', '15C'],
['temp9', '0'],
['temp10', '-17C']],
dtype='|S2'), array([['temp1', '110F'],
['temp2', '44C'],
['temp3', '0'],
['temp4', '0'],
['temp5', '300K'],
['temp6', '0'],
['temp7', '0'],
['temp8', '465R'],
['temp9', '0'],
['temp10', '0']],
dtype='|S2'), array([['temp1', '0'],
['temp2', '0', '0'],
['temp3', '91F', '0'],
['temp4', '14C', '284K'],
['temp5', '260K', '0'],
['temp6', '-10C', '458R'],
['temp7', '0', '0'],
['temp8', '-1C', '12F'],
['temp9', '0', '96F'],
['temp10', '0', '0']], dtype='|S2')]
my_output = [array([['temp1', '32F'],
['temp2', '243K'],
['temp6', '30C'],
['temp8', '15C'],
['temp10', '-17C']],
dtype='|S2'), array([['temp1', '110F'],
['temp2', '44C'],
['temp5', '300K'],
['temp8', '465R']],
dtype='|S2'), array([['temp1', '0', '423R'],
['temp3', '91F', '0'],
['temp4', '14C', '284K'],
['temp5', '260K', '0'],
['temp6', '-10C', '458R'],
['temp8', '-1C', '12F'],
['temp9', '0', '96F']], dtype='|S2')]
I tried
for columns in my_input:
for rows in columns:
for elements in rows:
if rows[1]&rows[2]==0|rows[1]&rows[2]&rows[3]==0:
my_output = np.delete(my_input,(rows[1]),axis=0)
Couldnt figure for n rows. Can anyone show how to accomplish this?
Upvotes: 1
Views: 140
Reputation: 425
In your proposed solution you are comparing with the integer 0, but the arrays consists of strings. So rows[1] == 0 will always give False
. There's other things wrong with your solution as well.
Putting that aside, here's something that should work:
output = []
for C in my_input:
mask = ~np.all(C[:, 1:] != '0', axis=1)
output.append(C[mask])
Note that ~ stands for elementwise negation.
A oneliner for this would be
output = [C[~np.all(C[:, 1:] != '0', axis=1)] for C in my_input]
Upvotes: 2