0

Here I have 2 arrays,

First array (arr1) :

array([[ 4.],[ 4.],[ 2.],[ 3.]])

Second array (arr2) :

array([[ 7.],[ 7.],[ 8.],[ 8.]])

I want to make a new dataframe from the two arrays, so it looks like this :

 arr1     arr2
   4       7
   4       7
   2       8
   3       8

I already made code like this, but it didn't work,

df = pd.DataFrame({'arr1':arr1, 'arr2':arrr2}, index=[0])

It says error : Exception: Data must be 1-dimensional

Thanks!

Hnp Mira
  • 11
  • 2

2 Answers2

0

It is error because your array is 2 dimensional (array of array). You can do the following to fix it:

# Create new array that is one dimensional
new_arr1 = [x[0] for x in arr1]
new_arr2 = [x[0] for x in arr2]
df = pd.DataFrame({'arr1':new_arr1, 'arr2':new_arr2})
Yosua
  • 411
  • 3
  • 7
0

According to me, arr1 and arr2 are data frames or ndarray it self and first need to be converted to array to be used to make data frame.

It should be like array([4,4,2,3]) to be accepted as valid values for data frame.

You can look for that in below link: https://stackoverflow.com/a/13730506/10798048

Correct me if I'm wrong

Dhruv Agarwal
  • 558
  • 6
  • 15