emax
emax

Reputation: 7255

Python: how to add columns to a numpy array?

I have an array that looks like the following

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]])

where

shape(X)
(1700, 3)

And I have a dataframe 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)

I would like to add the columns [20, 30, 40, 50, 60, 80, 90, 126, 200] to the array. I tried to add one column but I get an error

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)

Upvotes: 1

Views: 139

Answers (1)

imdevskp
imdevskp

Reputation: 2233

Try

a = df.loc[:, 20:120].to_numpy()
X = np.hstack((X, a))

Or

a = df.iloc[:, 2:].to_numpy()
X = np.hstack((X, a))

Upvotes: 2

Related Questions