1

I have a Pandas Dataframe (dataset, 889x4) and a Numpy ndarray (targets_one_hot, 889X29), which I want to concatenate. Therefore, I want to convert the targets_one_hot into a Pandas Dataframe.

To do so, I looked at several suggestions. However, these suggestions are about smaller arrays, for which it is okay to write out the different columns.

For 29 columns, this seems inefficient. Who can tell me efficient ways to turn this Numpy array into a Pandas DataFrame?

Emil
  • 1,531
  • 3
  • 22
  • 47

1 Answers1

2

We can wrap a numpy array in a pandas dataframe, by passing it as the first parameter. Then we can make use of pd.concat(..) [pandas-doc] to concatenate the original dataset, and the dataframe of the target_one_hot into a new dataframe. Since we here concatenate "vertically", we need to set the axis parameter on axis=1:

pd.concat((dataset, pd.DataFrame(targets_one_hot)), axis=1)
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555