r0f1
r0f1

Reputation: 3106

Pandas: Getting back original numpy array stored in DataFrame

Consider the following code:

import numpy as np
import pandas as pd

a = np.array([[1,2],[3,4],[5,6],[7,8]])
k = pd.DataFrame({"a": list(a)})

df

I'd like to retrieve the original numpy array. However, when I call .values I get something different.

values

How can I get the original numpy array, that looks like this:

array([[1, 2],
       [3, 4],
       [5, 6],
       [7, 8]])

?
Thank you.

Upvotes: 2

Views: 184

Answers (1)

jezrael
jezrael

Reputation: 862601

You can create nested lists and convert to 2d array:

np.array(k['a'].tolist())

Upvotes: 2

Related Questions