4

How do i convert a numpy array to a pandas dataframe?

for example:

test = np.array([[1,2],[2,3]])
test2 = np.array([[2,4],[2,5]])

to this:

pd.DataFrame({'test':[[1,2],[2,3]],
              'test2':[[2,4],[2,5]]})

     test   test2
0  [1, 2]  [2, 4]
1  [2, 3]  [2, 5]
3
  • 3
    It would be much better to have four separate columns with each containing one integer. That way, you could take advantage of the efficient vectorised operations such as sum(), diff(), etc. Commented Apr 30, 2015 at 21:42
  • I want to have lists in them because I am going to create a new column from test and test2 Commented Apr 30, 2015 at 21:43
  • it would still be a better practice to modify your approach to deal with single values in separate columns. Commented Apr 30, 2015 at 22:25

2 Answers 2

4

Although you could use

In [85]: pd.DataFrame({'test':test.tolist(), 'test2':test2.tolist()})
Out[85]: 
     test   test2
0  [1, 2]  [2, 4]
1  [2, 3]  [2, 5]

computation on the NumPy arrays would probably be much faster than an equivalent computation done on a Pandas DataFrame whose columns contain Python lists.

Sign up to request clarification or add additional context in comments.

Comments

0

If these numpy arrays have the same length, then Panel may be a preferable:

In [11]: p = pd.Panel({"test": test, "test2": test2})

In [12]: p
Out[12]:
<class 'pandas.core.panel.Panel'>
Dimensions: 2 (items) x 2 (major_axis) x 2 (minor_axis)
Items axis: test to test2
Major_axis axis: 0 to 1
Minor_axis axis: 0 to 1

In [13]: p["test"]  # a DataFrame
Out[13]:
   0  1
0  1  2
1  2  3

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.