-1

I have uploaded a dataset in Python. I need to do two things. One, change some of my variables, like ID, Gender (which is input as 0 and 1) from float64. I also want to change the input under gender from 0 to male and 1 to female.

I am not sure how to go about the first questions, but the second, I figure I would do a combination of a for loop and if then statements, but I am really confused. Any suggestions?

nekomatic
  • 5,988
  • 1
  • 20
  • 27
Deborah_Watson
  • 277
  • 1
  • 2
  • 8

1 Answers1

0

Since you didn't say what type you want to change your variables to, all I can suggest is to use casting.

x = 0
x_bool = bool(x) # False
x_str = str(x) # '0'
# ...

But, for Gender, I would suggest you to create an enum

from enum import Enum
class EGender(Enum):
    MALE = 0
    FEMALE = 1

Then you can do:

x = 0
x_gender = EGender(x) # EGender.MALE

EDIT: If you really want to have 'male' and 'female' (str types) for your gender attribute, you can use aenum to set multiple values for an enumerable. For more information about aenum, check this question

leoschet
  • 1,697
  • 17
  • 33