0

I am working with a dataframe with entirely numeric data, except 1 column which is labelled 'diagnosis', which has either an 'M' or a 'B'.

I wish to relabel all the B's as 0's and all the M's as 1's, but my code is not saving the results in the dataframe:

entries = df['diagnosis']

for i in entries:
    if i == 'B':
        i = 0
    else:
        i = 1

How can I achieve this and save the dataframe with the newly labelled data?

Greg
  • 65
  • 6
  • 3
    Use `df['entries'] = df['diagnosis'].replace({'B': 0, 'M': 1})` or `df['entries'] = np.where(df['diagnosis'].eq('B'), 0, 1)` – Henry Ecker Jun 22 '21 at 16:16

1 Answers1

1
df["diagnosis"].replace({"B": 0, "M": 1}, inplace=True)
Georgy Kopshteyn
  • 678
  • 3
  • 13