2

I have a data frame with an integer column representing severity values [1, 2, 3, 4]. I wish to convert this to an enumerated column with descriptive labels [Critical, High, Medium, Low]. Adding a new column as mentioned in this answer works, but I'd like to do this mapping in-place as I have a few more columns to be converted.

i.e. Convert this:

  Severity
0 1
1 2
2 1
...

to this:

  Severity
0 Critical
1 High
2 Critical
...

What is the best way to achieve this in Pandas?

Ishan De Silva
  • 935
  • 2
  • 8
  • 22

1 Answers1

1

Use Series.map:

a = [1, 2, 3, 4]
b = ['Critical', 'High', 'Medium', 'Low']

df['Severity'] = df['Severity'].map(dict(zip(a, b))

Or:

df['Severity'] = df['Severity'].map(dict(enumerate(b, 1))
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252