5

Supposing I have 20x100 numpy array. I want to select all columns except say 50th. So I was following this thread Extracting specific columns in numpy array but it didn't help. I tried using

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

but was giving error. In R it is easy to do this

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

But could not figure out in Python. Please help. Thanks

Community
  • 1
  • 1
Gaurav Chawla
  • 1,473
  • 3
  • 14
  • 19

4 Answers4

4

One way to get an R-like syntax here would be 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 from x.

DSM
  • 342,061
  • 65
  • 592
  • 494
1

This would work:

>>> a = np.arange(2000).reshape(20, 100)
>>> b = a[:, np.arange(a.shape[1]) != 50]
>>> b.shape
(20, 99)
Mike Müller
  • 82,630
  • 20
  • 166
  • 161
1

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]])
Mazdak
  • 105,000
  • 18
  • 159
  • 188
0

Alternatively, you can for 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))]
tagoma
  • 3,896
  • 4
  • 38
  • 57