0

I'm working the numpy librairy in Python 3. I would like to have a 3 dimensionnal array with the first two dimension named.

I have created a 3 dimensionnal array. I want to access the array like this

 my_3d_array["dim1_colnameX", "dim2_colnameX", 2:4]

I don't know if this is possible.

Here's what I've done:

import numpy as np
my_3d_array = np.zeros((2, 3, 5))

my_dtype = np.dtype(XXXX)

my_3d_array= my_3d_array.dtype(my_dtype)

print(my_3d_array)

Thank you!

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Jean
  • 1
  • 1
  • It's not exactly the same, but perhaps you could use ´enum.IntEnum` for indexing? – Paul Panzer Oct 23 '19 at 15:04
  • you should have a look at http://xarray.pydata.org/en/stable/, it is like pandas but with more dimension – Benoit de Menthière Oct 23 '19 at 15:23
  • I didn't think of an enum, maybe I'll do this. Or maybe an array of list (to add a third dimension) but I think your idea is more elegant. About xarray, I think it reveal that what I ask for is impossible... The inconvenient with xarray, is the code is for new people in python. I've already show them numpy and pandas, I don't think they'll like a third librairy for array. Anyhow, thanks a lot for your help. – Jean Oct 24 '19 at 14:22

1 Answers1

0

What do you want your output to look like?

You could transform your array into a pandas Dataframe and name the columns.

Take a look at this, on how to create a dataframe out of a 3d np.array

After that you can name and rename the columns like this:

#name
df.columns = ['column1', 'column2', 'column3']

#rename
df.rename(columns={'column1': 'dim1_colnameX'}, inplace=True)

You can read about this here or here

gradyhorn
  • 65
  • 8
  • Thanks for your help. It's not exactly what I want. Panda Dataframe are only with two dimensions, I want 3 dimension, that's why I go for Numpy ndarray. – Jean Oct 24 '19 at 14:10