3

I have a data frame wherein a column is of "object" data type. I use pd.to_numeric() with errors = 'coerce' to convert this to "float" data type. However, the converted column appears as NaN for all entries. If I let errors = 'ignore', none of the entries are converted to float. Is there something I am missing? The following is the code snippet:

pd.to_numeric(df['gender'],errors = 'coerce')

The column df['gender'] comprises 'Male' and 'Female' entries. I would like to convert these to 'float' data type.

Thank you!

1 Answer 1

2

to_numeric can only convert numeric-ish things. For example it can convert the string '10' into the number 10, but it can't convert something like 'Male' into a number.


Instead use pd.factorize:

df['gender'] = pd.factorize(df['gender'])[0].astype(float)

Or Series.factorize:

df['gender'] = df['gender'].factorize()[0].astype(float)

The first element of factorize contains the integer codes, so then we convert them astype(float).


Or as you commented, Series.map also works:

df['gender'] = df['gender'].map({'Male': 0, 'Female': 1}).astype(float)
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you! One quick comment: is this not the same as df.gender.replace({'Male': 1, 'Female': 0})? Nonetheless, it works fine.
Yep map is also fine, just a bit more manual. I also added an explanation for why to_numeric doesn't work here.
There is something glaring with pd.factorize(). The NaN entries in a given column are converted to '-1'. I tried including "na_sentinel=None", but it did not work. Doing so could be unfavorable in certain applications. Perhaps it is better to employ 'map' method, wherein NaN entries are retained and could be filled/replaced using imputation methods.
Good point if there are NaNs. In this case with only 2 categories + NaN, map would be simplest. If you had a ton of categories + NaN, you could factorize automatically and chain a replace, e.g. pd.Series(df['gender'].factorize()[0]).replace(-1, np.nan).

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.