0

I'm running into the error Shape of passed values is (2549950, 1), indices imply (2549950, 13) when trying to create pandas dataframe out of a One Hot encoded column.

Here's my code snippet.

ohe_df = pd.DataFrame(transformed, columns=enc.get_feature_names())

The shape of transformed is (2549950, 13) and shape of enc.get_feature_names() is (13,)

Thanks

1 Answers1

0

Can you try this

ohe_df = pd.DataFrame(transformed)

ohe.df.columns = enc.get_feature_names()
yeyosef
  • 78
  • 9
  • Tried this, I get the error : ValueError: Length mismatch: Expected axis has 1 elements, new values have 13 elements – Sneha Honnappa Mar 16 '22 at 15:10
  • transformed matrix is in sparse format as you commented above, change it to `pd.DataFrame` by using [this method](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.sparse.from_spmatrix.html) – yeyosef Mar 16 '22 at 15:16