0

The pandas data frame that I was working with has a column with string entries and I wish to convert it to integers.

The column is called diagnosis and each row has a value of either M or B. I wanted to convert all M to 1 and all B to 0 with the following code

for row in data['diagnosis']:
  if row == 'M':
    row = 1
  else:
    row = 0

But it does not change anything?

0

1 Answer 1

2

You can use map to do this

data['diagnosis'] = data['diagnosis'].map({'M':1,'B':0})
Sign up to request clarification or add additional context in comments.

Comments

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.