1
$\begingroup$

Was trying my hand at the Titanic dataset, when I wanted to One Hot Encode a categorical feature, after which I wanted to combine the original data with the new one hot vectors. The datatypes are as such:

data : Pandas Dataframe

Titles_ohe : Numpy sparse matrix (float64)

I tried to merge them into a dataframe using np.c_ :

columns = (list(data))+list(Titles.values)

data = pd.DataFrame(np.c_[data.values, Titles_ohe.toarray()], columns=columns)

However on checking the data type of the resulting Dataframe, all the attributes have been changed to the object datatype. Is there any way I can prevent this while using np.c_, or is there an alternative solution? Thanks in advance for any help!

$\endgroup$

1 Answer 1

2
$\begingroup$

I'd use DataFrame.join() in this case:

data = data.join(pd.SparseDataFrame(Titles_ohe, index=data.index, columns=Titles))
$\endgroup$
2
  • $\begingroup$ Thanks this worked. Could you tell me what the index attribute does? It’s not covered in the pandas documentation. $\endgroup$ May 4, 2018 at 16:43
  • $\begingroup$ @SureshKasipandy, this simply sets the index values of the dataframe, that we are creating... $\endgroup$ May 4, 2018 at 18:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.