Converting pandas.Multindex to numpy.ndarray with dtype float

Viewed 69

When converting a pandas.Multiindex to a numpy.ndarray, the output is a one dimensional ndarray with dtype=object as seen in the following example:

df = pd.DataFrame({
    'A': [10, 20, 30, 40, 50, 60],
    'B': [0,1,2,3,4,5],
    'C': ['K0', 'K1', 'K2', 'K3', 'K4', 'K5']
}).set_index(['A','B'])

The df will be:

A B C
10 0 K0
20 1 K1
30 2 K2
40 3 K3
50 4 K4
60 5 K5

The output for df.index.to_numpy() is a one dimensional ndarray with dtype=object:

array([(10, 0), (20, 1), (30, 2), (40, 3), (50, 4), (60, 5)], dtype=object)

but I want:

array([[10,  0],
       [20,  1],
       [30,  2],
       [40,  3],
       [50,  4],
       [60,  5]])

On How to convert a Numpy 2D array with object dtype to a regular 2D array of floats, I found the following solution:

np.vstack(df.index)

Is there any more direct or better solution?

2 Answers

I am pretty sure you will get what you want by flattening the multi index and taking numpy array from the result. E.g. by using the following syntax

np.array(list(df.index))

turn the index to columns.

df.reset_index()[['A', 'B']].values
Related