I want to change first column to int in dnarray
print(gcp)
[[ 1. 6.218 2.974 0. ] [ 2. 32.881 8.66 0. ] [ 3.
38.94 35.843 0. ] [ 4. 8.52 35.679 0. ] [ 5. 52.902 49.538 0. ]]
print(gcp.dtype)
float64
gcp[:,0] = (gcp[:,0]).astype(int) print(((gcp[:,0]).astype(int)).dtype)
int32
print(gcp.dtype)
float64
I tried to use deepcopy, but no success. Any help will be good, I didn’t find any similar questions.
Advertisement
Answer
To have just the index of the row(+1)
as element of the array seems redundant to me, maybe you don’t need the first row at all.
Otherwise the best option to me is to use two arrays:
gcp0 = gcp[:, 0].astype(int) gcp = gcp[:, 1:]
Another possibility is to define separate named fields
with respective dtypes
for the separate columns, which brings you maybe closer to pandas
, which is another option you have.
gcp = np.array([(1., 6.218, 2.974, 0.), (2., 32.881, 8.66, 0.), (3., 38.94, 35.843, 0.), (4., 8.52, 35.679, 0.), (5., 52.902, 49.538, 0.)], dtype={'names': ['index', 'a0', 'a1', 'a2'], 'formats': [int, float, float, float]}) print(gcp['index']) # [1 2 3 4 5]