tangerineGrapes
tangerineGrapes

Reputation: 11

How to change from an int to a string based on a condition

Let's say that I have a data frame with a list of names and their gender. The default data frame has either a "0" or a "1" in the gender column for each name of the person in which the "0" implies that the person is a male while the "1" implies that the person is a female. How do I change the zeros and the ones in the gender column to males and females?

Upvotes: 1

Views: 423

Answers (2)

Mad Physicist
Mad Physicist

Reputation: 114300

Direct indexing:

df['gender'] = np.array(['male', 'female'])[df.gender]

Upvotes: 0

devReddit
devReddit

Reputation: 2947

Just do:

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

OR

df.loc[(df.gender == 0), 'gender'] = 'male'
df.loc[(df.gender == 1), 'gender'] = 'female'

Upvotes: 1

Related Questions