I one hot encoded some variable and after some computation I would like to retrieve the original one.
What I am doing is the following:
I filter the one hot encoded column names (they all start with the name of the original variable, let say 'mycol'
)
filter_col = [col for col in df if col.startswith('mycol')]
Then I can simply multiply the column names by the filtered variables.
X_test[filter_col]*filter_col
However, this leads to a sparse matrix. How do I create one single variable out of this? Summing doesn't work as the empty spaces are treated as numbers and doing this: sum(X_test[filter_col]*filter_col)
I get
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Any suggestion on how to proceed? Is this even the best approach or is there some function out there doing exactly what I need?
As request, here is an example, taken from here:
df= pd.DataFrame({
'mycol':np.random.choice( ['panda','python','shark'], 10),
})
df=pd.get_dummies(df)