假设我有20x100 numpy数组。我想选择除50号之外的所有列。 所以我关注了这个帖子Extracting specific columns in numpy array 但它没有帮助。我尝试使用
x=Z[:,[:49,51:]]
但是给出了错误。在R中很容易做到这一点
x=Z[,c(1:49,51:100)]
但是在Python中无法弄明白。 请帮忙。感谢
答案 0 :(得分:4)
在这里获得类似R语法的一种方法是使用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])
我们发现x
中缺少第50列(编号49)。
答案 1 :(得分:1)
这样可行:
>>> a = np.arange(2000).reshape(20, 100)
>>> b = a[:, np.arange(a.shape[1]) != 50]
>>> b.shape
(20, 99)
答案 2 :(得分:1)
您只需使用np.delete()
删除第50列:
A = np.delete(A, 50, 1)
演示:
>>> 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]])
答案 3 :(得分:0)
或者,您可以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))]