Skip to content Skip to sidebar Skip to footer

Get_dummies(), Exception: Data Must Be 1-dimensional

I have this data I am trying to apply this: one_hot = pd.get_dummies(df) But I get this error: Here is my code up until then: # Import modules import pandas as pd import numpy a

Solution 1:

IMO, the documentation should be updated, because it says pd.get_dummies accepts data that is array-like, and a 2-D numpy array is array like (despite the fact that there is no formal definition of array-like). However, it seems to not like multi-dimensional arrays.

Take this tiny example:

>>> df
   ab  c
0a1  d
1b2  e
2  c  3  f

You can't get dummies on the underlying 2D numpy array:

>>>pd.get_dummies(df.values)

Exception: Data must be 1-dimensional

But you can get dummies on the dataframe itself:

>>>pd.get_dummies(df)
   b  a_a  a_b  a_c  c_d  c_e  c_f
0  1    1    0    0    1    0    0
1  2    0    1    0    0    1    0
2  3    0    0    1    0    0    1

Or on the 1D array underlying an individual column:

>>> pd.get_dummies(df['a'].values)
   ab  c
010010102001

Post a Comment for "Get_dummies(), Exception: Data Must Be 1-dimensional"