How to remove elements of one array depending nan element of another array in python?

Viewed 41

I want to remove all the nans from D1 or B1 and the same i-th elements from all the arrays (i.e. 2nd element of all array, so on)

import numpy as np
V1 = [ 7.98083 16.5216  18.4423  15.644   15.539   15.89    12.092   19.4274  14.953   15.943   13.665   16.777   15.901   13.477   15.563   11.217  15.415   16.023   16.855   14.851   14.419   12.345   14.234   16.515  16.148  ]
B1 = [ 8.75     nan 20.483 16.845 16.571 16.972 13.873    nan 16.238 16.625  14.715    nan 16.743 14.676    nan 12.578 16.459 17.148 20.313 15.801 15.462 13.998 15.421 20.129 18.055]
D1 = [ 132.024 3334.64       nan 1909.26  4256.32  2720.97  1926.14       nan  3612.81  5313.58  1444.71  2978.38  2400.28   999.693 3132.71   887.126  4574.86  1845.27  3905.06  2889.84  1687.51  2511.17  3176.06  2698.26  4980.22 ]

V = [[[x for x in D1 if ~np.isnan(x)] for x in B1 if 
~np.isnan(x)] for x in V1 if ~np.isnan(x)]; print(len(V))
B = [[[z for z in V1 if ~np.isnan(z)] for x in D1 if 
~np.isnan(x)] for y in B1 if ~np.isnan(y)]; print(len(B))
D = [[[y for y in B1 if ~np.isnan(y)] for z in V1 if 
~np.isnan(z)] for x in D1 if ~np.isnan(x)]; print(len(D))

But the number of elements in the arrays remain 25,21 and 23 instead of 21,21 and 21. Thanks in Advance

1 Answers

Your issue is that you have to process them all at once.

Let's assume the following input:

from numpy import nan
V1 = [7.98083, 16.5216, 18.4423, 15.644, 15.539, 15.89, 12.092, 19.4274, 14.953, 15.943, 13.665, 16.777, 15.901, 13.477, 15.563, 11.217, 15.415, 16.023, 16.855, 14.851, 14.419, 12.345, 14.234, 16.515, 16.148, ]
B1 = [8.75, nan, 20.483, 16.845, 16.571, 16.972, 13.873, nan, 16.238, 16.625, 14.715, nan, 16.743, 14.676, nan, 12.578, 16.459, 17.148, 20.313, 15.801, 15.462, 13.998, 15.421, 20.129, 18.055]
D1 = [132.024, 3334.64, nan, 1909.26, 4256.32, 2720.97, 1926.14, nan, 3612.81, 5313.58, 1444.71, 2978.38, 2400.28, 999.693, 3132.71, 887.126, 4574.86, 1845.27, 3905.06, 2889.84, 1687.51, 2511.17, 3176.06, 2698.26, 4980.22, ]

Solution with numpy:

a = np.array([V1, B1, D1])
V,B,D = a[:, ~np.isnan(a).any(0)]

Solution with pandas:

df = pd.DataFrame({'V1': V1, 'B1': B1, 'D1': D1})
df.dropna()

output:

          V1      B1        D1
0    7.98083   8.750   132.024
3   15.64400  16.845  1909.260
4   15.53900  16.571  4256.320
5   15.89000  16.972  2720.970
6   12.09200  13.873  1926.140
8   14.95300  16.238  3612.810
9   15.94300  16.625  5313.580
10  13.66500  14.715  1444.710
12  15.90100  16.743  2400.280
13  13.47700  14.676   999.693
15  11.21700  12.578   887.126
16  15.41500  16.459  4574.860
17  16.02300  17.148  1845.270
18  16.85500  20.313  3905.060
19  14.85100  15.801  2889.840
20  14.41900  15.462  1687.510
21  12.34500  13.998  2511.170
22  14.23400  15.421  3176.060
23  16.51500  20.129  2698.260
24  16.14800  18.055  4980.220
Related