2

I have a dataframe looks like this

   survived pclass  sex age sibsp   parch   fare    embarked
    0   1   1   female  29.0000 0   0   211.3375    S
    1   1   1   male    0.9167  1   2   151.5500    S
    2   0   1   female  2.0000  1   2   151.5500    S
    3   0   1   male    30.0000 1   2   151.5500    S
    4   0   1   female  25.0000 1   2   151.5500    S

I want to convert 'sex' to 0, 1 coding and used isnull checked that there is no NA in the column

However, on this line I received ValueError: Cannot convert non-finite values (NA or inf) to integer

df['sex'] = df['sex'].map({'female':0, 'male':1}).astype(int)

Any suggestions ? thank you !

Osca
  • 1,588
  • 2
  • 20
  • 41
  • No, if want integer with NaNs values it is problem, check dupe. Possible solution is remove all NaNs rows by column `Sex` or replace `NaN` to some integer like `fillna(2).astype(int)` – jezrael Oct 29 '18 at 07:13

2 Answers2

3

Use np.where

Ex:

import numpy as np

df['sex'] = np.where(df['sex'] == 'female', 0, 1)
Rakesh
  • 81,458
  • 17
  • 76
  • 113
2

I think the proper way to do it is by using the replace function

df.replace({'sex':{'female':0, 'male':1}}, inplace=True)

If your df has nans, then you could fill them by some value, e.g. -1, using fillna and then replace the rest

df.fillna({'sex':-1}, inplace=True)
df.replace({'sex':{'female':0, 'male':1}}, inplace=True)
Andreas K.
  • 9,282
  • 3
  • 40
  • 45