Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 13891

Delete Columns with same elements

$
0
0
arr_2d = np.array([[ 1,  2,  4,  9, 3, 10],                   [ 1,  3,  4, 12, 3, 15],                   [ 1,  6,  4, 16, 3, 22],                   [ 1, 10,  4, 21, 3, 30]])arr_2d_copy = arr_2d.copy()for col_idx in range(arr_2d.shape[1]):    are_all_same = np.all(arr_2d[:,col_idx] == arr_2d[:,col_idx][0])    print('Same? ', are_all_same)    if are_all_same:        arr_2d_copy = np.delete(arr_2d_copy, col_idx, axis=1)print(arr_2d_copy)

I am trying to get an output like this -

array([[ 2,  9, 10],       [ 3, 12, 15],       [ 6, 16, 22],       [10, 21, 30]])

My code clearly isn't working because of indexing error.


Viewing all articles
Browse latest Browse all 13891

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>