0

I am trying to assigns numbers to the 'Sex' feature in a dataframe. My code is as follows:

df['Sex'] = df['Sex'].replace('male', 1, inplace = True)
df['Sex'] = df['Sex'].replace('female', 2, inplace = True)

However, the code gives as outcome of 'None' in the 'Sex' feature of the dataframe. What could be the problem in this code?

Ahmed Jyad
  • 45
  • 4

1 Answers1

0

I think you are using pandas DataFrame replace when instead you want pandas .str.replace. If I'm not mistaken this should work:

df['sex'] = df['sex'].str.replace('male',1)
df['sex'] = df['sex'].str.replace('female',2)
Dishin H Goyani
  • 7,195
  • 3
  • 26
  • 37
Celius Stingher
  • 17,835
  • 6
  • 23
  • 53