Skip to content
Advertisement

Substitute column values of a dataframe with the corresponding items in an array

I have a column in a dataframe which contains an array of numbers from 1 to 5 and I have an array containing five words. I would like to find the simplest, most compact and most elegant way in Python to “in place” replace the numbers in the column with the corresponding words. For example:

JavaScript

I am certain that the numbers in columns “color” are not NaN or outside range [1,5]. No check is necessary. Any suggestion?

Advertisement

Answer

This should do

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement