1

I first created an array:

nlist= [[[1,2,3],[4,5,6],[7,8,9]],[[10,11,12], [13,14,15], [16,17,18]],[[19,20,21],[22,23,24],[25,26,27]]]
import numpy as np
narray = np.array(nlist)

Then I converted it into a dataframe using:

import pandas as pd
df = pd.DataFrame.from_records(narray)

So I basically converted a 3-dimensional array to a 3-dimensional dataframe. Now when I try to get it back as an array using:

new_array = np.array(df)

Now this returns a 2-dimensional array (used new_array.shape to check dimensions) . But I want the original 3-dimensional array. What do I do?

1
  • Show the dataframe (atleast a few rows) and new_array. With dtypes. So both of us understand what is happening. A normal dataframe is 2d. Commented Jan 23, 2020 at 12:12

1 Answer 1

1

try this,

new_array = np.array(df.values.tolist())



print(narray)
print(type(narray))
df = pd.DataFrame.from_records(narray)
print(df)
**new_array = np.array(df.values.tolist())**
print((new_array))
print(type(new_array))

O/P:

[[[ 1  2  3]
  [ 4  5  6]
  [ 7  8  9]]

 [[10 11 12]
  [13 14 15]
  [16 17 18]]

 [[19 20 21]
  [22 23 24]
  [25 26 27]]]
<class 'numpy.ndarray'>
              0             1             2
0     [1, 2, 3]     [4, 5, 6]     [7, 8, 9]
1  [10, 11, 12]  [13, 14, 15]  [16, 17, 18]
2  [19, 20, 21]  [22, 23, 24]  [25, 26, 27]
[[[ 1  2  3]
  [ 4  5  6]
  [ 7  8  9]]

 [[10 11 12]
  [13 14 15]
  [16 17 18]]

 [[19 20 21]
  [22 23 24]
  [25 26 27]]]
<class 'numpy.ndarray'>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.