Python:如何向numpy数组添加列?
我有一个数组,如下所示Python:如何向numpy数组添加列?,python,arrays,pandas,dataframe,numpy,Python,Arrays,Pandas,Dataframe,Numpy,我有一个数组,如下所示 X array([[ 0.93387437, -0.6063677 , -1.1959038 ], [ 0.53421287, -0.31532495, -1.06524933], [ 0.68792884, 0.34513742, -0.90179775], ..., [-0.41882609, -0.17155083, 1.14911752], [-0.04990778, 0.83636566
X
array([[ 0.93387437, -0.6063677 , -1.1959038 ],
[ 0.53421287, -0.31532495, -1.06524933],
[ 0.68792884, 0.34513742, -0.90179775],
...,
[-0.41882609, -0.17155083, 1.14911752],
[-0.04990778, 0.83636566, 0.92968195],
[-0.48031247, 0.37908409, 1.00781703]])
在哪里
我有一个数据帧df
geometry data 20 30 40 50 60 80 90 126 200
0 POINT (-17.57616 14.87086) 200 0 0 0 0 0 0 0 0 1
1 POINT (-17.56784 14.87086) 200 0 0 0 0 0 0 0 0 1
2 POINT (-17.55952 14.87086) 200 0 0 0 0 0 0 0 0 1
3 POINT (-17.55119 14.87086) 200 0 0 0 0 0 0 0 0 1
4 POINT (-17.54287 14.87086) 200 0 0 0 0 0 0 0 0 1
shape(df1)
(1700, 11)
我想将列[20、30、40、50、60、80、90、126、200]
添加到数组中。我试图添加一列,但出现错误
a = np.array(df1.iloc[:,4])
X = np.hstack((X, a))
ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)
试一试
或
我收到一个错误
KeyError:'20'
列名'20'看起来像一个字符串,它是'int',请查看更新的答案
a = np.array(df1.iloc[:,4])
X = np.hstack((X, a))
ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)
a = df.loc[:, 20:120].to_numpy()
X = np.hstack((X, a))
a = df.iloc[:, 2:].to_numpy()
X = np.hstack((X, a))