Setthawut Kulsrisuwan
Setthawut Kulsrisuwan

Reputation: 524

ValueError: Must pass 2-d input. shape=(

Our objective is to create a data frame with a shape of (2,3,2) as follows:

  1. first index or 2 = row in the data frame

  2. second index or 3 = columns in the data frame

  3. third index or 2 = values in those rows and columns.

Our data:

data = np.array([[[1,3],[5,7],[9,11]],[[2,4],[6,8],[10,12]]])
print(data.shape)
print("=====================================")
data

When add to a data frame as codes below:

df_sample = pd.DataFrame(data,columns=['A','B','C'])

it shows: ValueError: Must pass 2-d input. shape=(2, 3, 2)

or can see as an image below:

enter image description here

Upvotes: 3

Views: 21041

Answers (1)

Linden
Linden

Reputation: 571

Does this work?

data = np.array([[[1,3],[5,7],[9,11]],[[2,4],[6,8],[10,12]]])
pd.DataFrame(data.tolist(), columns=['A','B','C'])

returns

    A        B       C
0   [1, 3]  [5, 7]  [9, 11]
1   [2, 4]  [6, 8]  [10, 12]

Upvotes: 4

Related Questions