How to select specific columns in a numpy array?

Suppose I have a 20x100 numpy array. I want to select all columns except the 50th. So, I followed this topic Retrieving specific columns in a numpy array but this did not help. I tried to use

x=Z[:,[:49,51:]] 

but gave an error. In R it's easy to do

 x=Z[,c(1:49,51:100)] 

But failed to find out in Python. Please help. Thanks

+5
source share
4 answers

One way to get R-like syntax here is to use np.r_ :

 >>> Z = np.arange(2000).reshape(20, 100) >>> Z.shape (20, 100) >>> x = Z[:,np.r_[:49,50:100]] >>> x.shape (20, 99) >>> x[0,48:52] array([48, 50, 51, 52]) 

and we see that the 50th column (with number 49) is missing at x .

+4
source

This will work:

 >>> a = np.arange(2000).reshape(20, 100) >>> b = a[:, np.arange(a.shape[1]) != 50] >>> b.shape (20, 99) 
+1
source

You can simply delete the 50th column using np.delete() :

 A = np.delete(A, 50, 1) 

Demo:

 >>> import numpy as np >>> A = np.arange(100).reshape(25,4) >>> A array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23], [24, 25, 26, 27], [28, 29, 30, 31], [32, 33, 34, 35], [36, 37, 38, 39], [40, 41, 42, 43], [44, 45, 46, 47], [48, 49, 50, 51], [52, 53, 54, 55], [56, 57, 58, 59], [60, 61, 62, 63], [64, 65, 66, 67], [68, 69, 70, 71], [72, 73, 74, 75], [76, 77, 78, 79], [80, 81, 82, 83], [84, 85, 86, 87], [88, 89, 90, 91], [92, 93, 94, 95], [96, 97, 98, 99]]) >>> >>> A = np.delete(A, 2, 1) >>> A array([[ 0, 1, 3], [ 4, 5, 7], [ 8, 9, 11], [12, 13, 15], [16, 17, 19], [20, 21, 23], [24, 25, 27], [28, 29, 31], [32, 33, 35], [36, 37, 39], [40, 41, 43], [44, 45, 47], [48, 49, 51], [52, 53, 55], [56, 57, 59], [60, 61, 63], [64, 65, 67], [68, 69, 71], [72, 73, 75], [76, 77, 79], [80, 81, 83], [84, 85, 87], [88, 89, 91], [92, 93, 95], [96, 97, 99]]) 
+1
source

Alternatively you can use iloc

 import numpy as np import pandas as pd data = np.random.normal(size=2000).reshape(20, 100) df = pd.DataFrame(data, columns=list(range(1,101))) df.iloc[:,list(range(49)) + list(range(50, 100))] 
0
source

Source: https://habr.com/ru/post/1239301/


All Articles