I have a data frame like the following:
test = {"viral": "pos", "Status": "positive", "Age": 59, "score": 5}
test2 = {"viral": "neg"}
df = pd.DataFrame.from_dict([test, test2])
I want to encode the character columns (viral, Status) as ordinal numbers, but leave the numeric values alone. Desired output is a numpy array with the same column names.
If I use OrdinalEncoder from sklearn, it does not deal with NaN values. Even if there were not NaN values, it would still ordinal encode the numeric columns also however. I want to fill NaN values with 0 in character columns but keep NaN in numeric columns.
What is the easiest way to do this?
Desired output (in numpy array):
Age Status score viral
0 59.0 1 5.0 1
1 NaN 0 NaN 0
Thanks! Jack
EDIT: I would also like a mapping from encoded value to original value like {i: dict(enumerate(v)) for i, v in enumerate(enc.categories_)} when using enc=OrdinalEncoder() (see Vectorize 2D character array column-wise)