0

I'm trying to convert my numpy array into a dataframe,

array:

array([[[9.447859e+06, 3.000000e+00, 1.350000e+01, 7.000000e+00]]

   [[9.447859e+06, 2.000000e+00, 1.350000e+01, 4.000000e+00]],

   [[9.447859e+06, 1.000000e+00, 1.350000e+01, 7.000000e+00]]])

expected output:

A            B            C            D
9.447859e+06 3.000000e+00 1.350000e+01 7.000000e+00
9.447859e+06 2.000000e+00 1.350000e+01 4.000000e+00
9.447859e+06 1.000000e+00 1.350000e+01 7.000000e+00

can I get a solution for this? Thanks.

1
  • 1
    What have you tried? How did it fail? Commented May 4, 2021 at 11:54

4 Answers 4

3

You can use numpy.squeeze() to remove axes of length one from array.

df = pd.DataFrame(np.squeeze(data), columns=['A', 'B', 'C', 'D'])
Sign up to request clarification or add additional context in comments.

1 Comment

Best Readability and easy to initialize.
1

Try this

import numpy as np
a = np.array([[[1, 2,3, 4]], [[5, 6, 7, 8]]])

m,n,r = a.shape
out_arr = a.reshape(m*n,-1)
out_df = pd.DataFrame(out_arr,columns =['A','B','C','D'])
out_df

Result

    A   B   C   D
0   1   2   3   4
1   5   6   7   8

Comments

0

You can specify which part of the array to use as data:

pd.DataFrame(data=a[:,0], index=range(0, len(a)), columns=['A', 'B', 'C', 'D'])

Comments

0

This works (after removing the double brackets):

import pandas as pd
import numpy as np
a = np.array([[9.447859e+06, 3.000000e+00, 1.350000e+01, 7.000000e+00],

   [9.447859e+06, 2.000000e+00, 1.350000e+01, 4.000000e+00],

   [9.447859e+06, 1.000000e+00, 1.350000e+01, 7.000000e+00]])

b = pd.DataFrame(data=a )
b

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.